0% found this document useful (0 votes)
7 views12 pages

Examples JavaScript

The document contains multiple HTML examples demonstrating various JavaScript functionalities, including factorial calculations, variable scope, array usage, form validation, and event handling. Each example illustrates different programming concepts such as primitive vs reference data types, form submission alerts, and input validation for numeric ranges. Overall, it serves as a tutorial for basic JavaScript programming within HTML documents.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

Examples JavaScript

The document contains multiple HTML examples demonstrating various JavaScript functionalities, including factorial calculations, variable scope, array usage, form validation, and event handling. Each example illustrates different programming concepts such as primitive vs reference data types, form submission alerts, and input validation for numeric ranges. Overall, it serves as a tutorial for basic JavaScript programming within HTML documents.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

<html>

<body>

<script language="JavaScript">
// example from Flanagan (1998)

document.write("<h2>Table of factorials</h2>");
for (i=1, fact=1; i < 10; i++, fact *=i) {
document.write(i + "! = " + fact);
document.write("<br>");
}

</script>

</body>
</html>
<html>
<body>

<script language="JavaScript">
// example modified from Flanagan (1998)

x = "global";
var y = "global";

function checkscope() {
var y = "local";
document.write("y= ", y);
document.write(" x= ", x);
}
checkscope();
</script>

</body>
</html>
<html>

<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Hide script from old browsers
function square(number)
{
return number * number;
}
// End script hiding from old browsers -->
</SCRIPT>
</HEAD>

<BODY>
<SCRIPT>
<!-- Hide script from old browsers
document.write("The function returned ",
square(5), ".");
// End script hiding from old browsers -->
</SCRIPT>

<p>All done.

</BODY>

</html>
<html>

<head>
<script LANGUAGE="JavaScript">
var width
width = 25
var car = "ford"
car = 25
car = 2001 + " Toyota"
</script>
</head>

<BODY>
<script>
document.write("value of variable ", width, "<p>");
document.write('value of other variable ', car);
</script>
</BODY>

</html>
<html>
<body>

<script>
var a = new Array();
a[10] = "my name is Sue";
var b = [1.2, "JavaScript", false];
b[10] = "my name is not Sue";

document.write("a[10]= ", a[10], " b[10]= ", b[10]);


</script>

</body>
</html>
<html>
<body>

<script>
// example modified from Flanagan (1998)

// "primitive" data example


var a = 3.14;
var b = a;
a = 4;
alert(b);

// "reference" data example


var x = [1, 2, 3];
var y = x;
x[0] = 99;
alert(y);

</script>

</body>
</html>
<html>
<body>

<FORM name= "form1">


<P>
First name:
<INPUT type="text" name="firstname"><BR>
<input type="file"><br>
<INPUT type="radio" name="sex"
value="Male"> Male<BR>
<INPUT type="radio" name="sex"
value="Female"> Female<p>
<INPUT name= "checkbox"
type="checkbox">Window seat <br>
<INPUT type="submit" name="submit" value="Send">
<INPUT type="reset"> <br>

</P>
</FORM>

<script>
// You can now attach JavaScript event handlers to
// the HTML form elements
document.form1.submit.onclick = function() {alert('Thanks!'); }

</script>
</body>
</html>
<HTML>
<HEAD>
<TITLE>JavaScript Demo Example 9</TITLE>
<script language="javascript" type="text/javascript">
<!-- Hide script from older browsers

function Validate() {
var numberEntered = document.numberForm.valueEntered.value;

if( isNaN(numberEntered)) {
alert("You entered a non-digit character. “,
“Please try again.") }
else if( (numberEntered<1) || (numberEntered>50) ) {
alert("The number you entered is not between 1 and 50. ",
" Please try again.") }
else {
alert("Thank you for entering a number between 1 and 50.")
}
document.numberForm.valueEntered.value = "";
document.numberForm.valueEntered.focus();
}

//-->
</script>

</HEAD>
<BODY BGCOLOR="WHITE">

<h3>Validate data and provide immediate feedback.</h3>


<form name="numberForm">
<p><b>Please enter a number between 1 and 50
and click the Enter button.</b></p>
<input type="text" name="valueEntered" size="5"
onchange="Validate();">
<input type="button" name="Enter" action="none" value="Enter">
</form>
</BODY>
</HTML>
<HTML>
<HEAD>
<TITLE>validate2.htm</TITLE>
<script language="javascript" type="text/javascript">
<!-- Hide script from older browsers

function Validate() {
var numberEntered = document.numberForm.valueEntered.value;
if( isNaN(numberEntered)) {
alert("You entered a non-digit character. ",
"Please try again.") }
else if( (numberEntered<1) || (numberEntered>50) ) {
alert("The number you entered is not between 1 and 50.",
" Please try again.")
} else {
alert("Thank you for entering a number between 1 and 50.")
}
document.numberForm.valueEntered.value = "";
}

//-->
</script>

</HEAD>
<BODY BGCOLOR="WHITE">
<h3>Validate data and provide immediate feedback.</h3>
<form name="numberForm">
<p><b>Please enter a number between 1 and 50 and click the Enter
button.</b></p>
<input type="text" name="valueEntered" size="5"
>
<input type="button" name="Enter" action="none" value="Enter">
</form>

<script>
// example usage without parameter to function
document.numberForm.valueEntered.onchange =
new Function("Validate();");
</script>
</BODY></HTML>
<HTML>
<HEAD>
<TITLE>validate3.htm</TITLE>
<script language="javascript" type="text/javascript">
<!-- Hide script from older browsers

function Validate() {
var numberEntered = document.numberForm.valueEntered.value;
var tmp = "value = " + numberEntered;
alert(tmp);
if( isNaN(numberEntered)) {
alert("You entered a non-digit character. ",
"Please try again.") }
else if( (numberEntered<1) || (numberEntered>50) ) {
alert("The number you entered is not between 1 and 50.",
" Please try again.")
} else {
alert("Thank you for entering a number between 1 and 50.")
}
document.numberForm.valueEntered.value = "";
}

//-->
</script>
</HEAD>
<BODY BGCOLOR="WHITE">
<h3>Validate data and provide immediate feedback.</h3>
<form name="numberForm">
<p><b>Please enter a number between 1 and 50 and click the Enter
button.</b></p>
<input type="text" name="valueEntered" size="5"
>
<input type="button" name="Enter" action="none" value="Enter">
</form>

<script>
// example usage without parameter to function
document.numberForm.valueEntered.onchange= Validate;
</script>
</BODY></HTML>
<HTML>
<HEAD>
<TITLE>alert.htm examples of problems in parameter passing</TITLE>
<script language="javascript" type="text/javascript">
<!-- Hide script from older browsers

function Validate() {
var numberEntered = document.numberForm.valueEntered.value;
// this will not work: Alert only looks at the first parameter
alert("number is (first attempt): ", numberEntered);

// however, this will work


alert("number is (second attempt): " + numberEntered);
}

//-->
</script>

</HEAD>
<BODY BGCOLOR="WHITE">

<h3>Show an incorrect usage of parameter passing to alert.</h3>


<form name="numberForm">
<p><b>Please enter a number.</b></p>
<input type="text" name="valueEntered" size="5"
onchange="Validate();">
</form>
</BODY>
</HTML>
<html>
<body>

<a href = "http://www.cprince.com"


onMouseOver=
"status='Go to my home page'; return true;"
onMouseOut="status='';">
home page </a>
</body>
</html>

You might also like