Web CH 4 JavaScript I
Web CH 4 JavaScript I
<body>
<script language="JavaScript">
</script>
</body>
</html>
Compiled By Aliazar D. (MSc in SEng) 6
Conti..
<html>
• JavaScript can be located in the head, <head>
body or external file <script src="xxx.js"></script>
◙ Head section or
• Ensures script is loaded before <script type="text/javascript">
trigger event ....
◙ Body section </script>
• Script executes when body loads </head>
◙ External <body>
• Allows scripts to run on several <script type="text/javascript">
pages ....
</script>
</body>
Compiled By Aliazar D. (MSc in SEng) 7
Conti..
<html>
<head>
<script type="text/javascript">
function sayHello()
{
alert("Hello World")
}
</script>
</head>
<body>
<script type="text/javascript">
document.write("Hello World")
</script>
<br>
<input type="button" onclick="sayHello()" value="Say Hello"/>
</body>
</html>
13
Compiled By Aliazar D. (MSc in SEng)
Example
<html>
<head>
<title>JavaScript HelloWorld!</title>
</head>
<script language='JavaScript'>
var x = 3;
var y = 10;
document.write(typeof x + "<br/>") ;
document.write(x++ * ++y + y +x + "<br/>") ; //(3*11)+11+4
x = 3;
y = 10;
document.write(++x * ++y + y /x + y + "<br/>") ; //(4*11)+(11/4)+(10)
x = 3;
y = 10;
document.write( ++x * y++ + y +x + "<br/>" ) ;//4*10+11+4
document.write ( "y = " + y + " x = " + x );
</script>
Compiled By Aliazar D. (MSc in SEng) 14
</body>
JavaScript Operators:
Automatic Type Conversion
• Here's how you would access all values inside an array by looping over each of them:
<html>
<body>
<script type="text/javascript">
var colors = ["green","red","yellow","orange","blue"];
for (var i=0;i< colors.length;i++)
{
document.write(colors[i]+"<br />");
}
</script>
</body>
</html>
26
❖ Once you access a value inside an array you can simply retrieve it, as you did in
the previous example, or you can modify it, as follows:
var colors = ["green", "red", "yellow", "orange", "blue"];
colors[2] = "pink";
❖ Now you've replaced the item at index position 2, the third item called "yellow”,
with "pink“
document.write(colors);
❖ The code should print: green, red, pink, orange, blue ("pink" has replaced
"yellow")
27
Working with Program Loops
num ++;
}
</script>
</tr>
</table>
var x = 1, y = 4, z = 2;
Syntax:
return expression;
• Function can have multiple return statements but only the first can be executed in any
given function call
• Normally used to return the final result of a calculation to the calling program
function writeHello() {
document.write(globalHello);
}
// outputs “Hello!”
<html>
<head>
<script type="text/javascript">
<!--
function sayHello()
{
alert("Hello World")
}
//-->
</script>
</head>
<body>
<input type="button" onclick="sayHello()" value="Say Hello" />
</body>
</html> 43
JavaScript Form Validation:
44
EXAMPLE:
▪ The function below checks if a field has been left empty. If the field is blank, an alert box
alerts a message, the function returns false, and the form will not be submitted:
<!DOCTYPE html>
<html>
<head>
<script>
function validateForm()
{
var x=document.myForm.fname.value;
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
}
</script>
</head>
<body>
<form name="myForm" action="demo_form.php" onsubmit="return validateForm()" method="post">
First name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</body>
</html> 45
Conti..
• Built-In Functions
►Prompt
►Alert
►Confirm
<p id="demo"></p>
</body>
<script>
function myFunction() {
let person = prompt("Please enter your name", "Lazarus D");
if (person != null) {
document.getElementById("demo").innerHTML =
"Hello " + person + "! How are you today?";
}
}
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<script>
function myFunction() {
alert("Hello! I am an alert box!");
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
function myFunction() {
let text = "Press a button!\nEither OK or Cancel.";
if (confirm(text) == true) {
text = "You pressed OK!";
} else {
text = "You canceled!";
}
document.getElementById("demo").innerHTML = text;
}
</script>
</body>
</html>
49
Compiled By Aliazar D. (MSc in SEng)
Compiled By Aliazar D. (MSc in SEng) 50