29
Lecture 8 Kanida Sinmai [email protected] http://mis.csit.sci.tsu.ac.th/kanida JavaScript – part

JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

Embed Size (px)

Citation preview

Page 1: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

Lecture 8Kanida Sinmai

[email protected]://mis.csit.sci.tsu.ac.th/kanida

JavaScript – part II

Page 2: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

The if Statement if (condition) {    block of code to be executed if the condition is true}

Page 3: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

Example<!DOCTYPE html><html><body>

<p>Display "Good day!" if the hour is less than 18:00:</p>

<p id="demo">Good Evening!</p>

<script>if (new Date().getHours() < 18) { document.getElementById("demo").innerHTML = "Good day!";}</script>

</body></html>

Page 4: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

If … else statementif (condition) {    block of code to be executed if the condition is true} else {     block of code to be executed if the condition is false}

Page 5: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

Example<!DOCTYPE html><html><body><p>Click the button to display a time-based greeting:</p><button onclick="myFunction()">Try it</button><p id="demo"></p><script>function myFunction() { var hour = new Date().getHours(); var greeting; if (hour < 18) { greeting = "Good day"; } else { greeting = "Good evening"; } document.getElementById("demo").innerHTML = greeting;}</script></body></html>

Page 6: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

The JavaScript Switch Statementswitch(expression) { case n:        code block break; case n:        code block break; default: default code block}

Page 7: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

Example<!DOCTYPE html><html><body> <p id="demo"></p>

<script>var day;switch (new Date().getDay()) { case 0: day = "Sunday"; break; case 1: day = "Monday"; break; case 2: day = "Tuesday"; break; case 3: day = "Wednesday"; break; case 4: day = "Thursday"; break; case 5: day = "Friday"; break; case 6: day = "Saturday"; break;}document.getElementById("demo").innerHTML = "Today is " + day;</script>

</body></html>

Page 8: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

The For Loopfor (statement 1; statement 2; statement 3) { code block to be executed}

Page 9: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

Example<!DOCTYPE html><html><body>

<p id="demo"></p>

<script>var cars = ["BMW", "Volvo", "Saab", "Ford"];var i, len, text;for (i = 0, len = cars.length, text = ""; i < len; i++) { text += cars[i] + "<br>";}

document.getElementById("demo").innerHTML = text;</script>

</body></html>

Page 10: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

The While Loopwhile (condition) {    code block to be executed}

Page 11: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

Example<!DOCTYPE html><html><body>

<p>Click the button to loop through a block of code as long as i is less than 10.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>function myFunction() { var text = ""; var i = 0; while (i < 10) { text += "<br>The number is " + i; i++; } document.getElementById("demo").innerHTML = text;}</script>

</body></html>

Page 12: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

The do..while Loopdo {    code block to be executed}while (condition);

Page 13: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

Example <!DOCTYPE html><html><body>

<p>Click the button to loop through a block of code as long as i is less than 10.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>function myFunction() { var text = "" var i = 0; do { text += "<br>The number is " + i; i++; } while (i < 10) document.getElementById("demo").innerHTML = text;}</script>

</body></html>

Page 14: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

isNan() Function <!DOCTYPE html><html><body>

<p>Click the button to check whether a number is an illegal number.</p><button onclick="myFunction()">Try it</button><p id="demo"></p>

<script>function myFunction() { var a = isNaN(123) + "<br>"; var b = isNaN(-1.23) + "<br>"; var c = isNaN(5-2) + "<br>"; var d = isNaN(0) + "<br>"; var e = isNaN("123") + "<br>"; var f = isNaN("Hello") + "<br>"; var g = isNaN("2005/12/12");

var res = a + b + c + d + e + f + g; document.getElementById("demo").innerHTML = res;}</script>

</body></html>

• The isNaN() function returns true if the value is NaN (Not-a-Number), and false if not.

Page 15: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

Input Validation Example

<!DOCTYPE html><html><body>

<p>Please input a number between 5 and 10:</p>

<input id="demo" type="text"><button type="button" onclick="myFunction()">Test Input</button><p id="message"></p>

<script>function myFunction() { var message, x; message = document.getElementById("message"); message.innerHTML = ""; x = document.getElementById("demo").value; try { if(x == "") throw "empty"; if(isNaN(x)) throw "not a number"; x = Number(x); if(x < 5) throw "too low"; if(x > 10) throw "too high"; } catch(err) { message.innerHTML = "Input is " + err; }}</script>

</body></html>

Page 16: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

Fine the Value of the elements

<!DOCTYPE html><html><body>

<form id="frm1" action="form_action.asp"> First name: <input type="text" name="fname" value="Donald"><br> Last name: <input type="text" name="lname" value="Duck"><br><br> <input type="submit" value="Submit"></form>

<p>Click "Try it" to display the value of each element in the form.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>function myFunction() { var x = document.getElementById("frm1"); var text = ""; var i; for (i = 0; i < x.length ;i++) { text += x.elements[i].value + "<br>"; } document.getElementById("demo").innerHTML = text;}</script>

</body></html>

Page 17: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

Submit a Form <!DOCTYPE html><html><body>

<p>Enter names in the fields, then click "Submit" to submit the form:</p>

<form id="frm1" action="form_action.php"> First name: <input type="text" name="fname"><br> Last name: <input type="text" name="lname"><br><br> <input type="button" onclick="myFunction()" value="Submit"></form>

<script>function myFunction() { document.getElementById("frm1").submit();}</script>

</body></html>

It doesn’t work at the moment as you need form_action.php

Page 18: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

Reset a Form<!DOCTYPE html><html><body>

<p>Enter names in the fields below, then click "Reset" to reset the form.</p>

<form id="frm1"> First name: <input type="text" name="fname"><br> Last name: <input type="text" name="lname"><br><br> <input type="button" onclick="myFunction()" value="Reset"></form>

<script>function myFunction() { document.getElementById("frm1").reset();}</script>

</body></html>

Page 19: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

JavaScript Events: onblur<!DOCTYPE html><html><head><script>function myFunction() { var x = document.getElementById("fname"); x.value = x.value.toUpperCase();}</script></head><body>

Enter your name: <input type="text" id="fname" onblur="myFunction()">

<p>When you leave the input field, a function is triggered which transforms the input text to upper case.</p>

</body></html>

Page 20: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

JavaScript Event: onchange<!DOCTYPE html><html><head><script>function myFunction() { var x = document.getElementById("fname"); x.value = x.value.toUpperCase();}</script></head><body>

Enter your name: <input type="text" id="fname" onchange="myFunction()"><p>When you leave the input field, a function is triggered which transforms the input text to upper case.</p>

</body></html>

Page 21: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

JavaScript Event: onfocus<!DOCTYPE html><html><head><script>function myFunction(x) { x.style.background = "yellow";}</script></head><body>

Enter your name: <input type="text" onfocus="myFunction(this)">

<p>When the input field gets focus, a function is triggered which changes the background-color.</p>

</body></html>

Page 22: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

JavaScript Event: onsubmit<!DOCTYPE html><html><head><script>function confirmInput() { fname = document.forms[0].fname.value; alert("Hello " + fname + "! You will now be redirected to www.w3Schools.com");}</script></head>

<body><form onsubmit="confirmInput()" action="http://www.w3schools.com/">Enter your name: <input id="fname" type="text" size="20"><input type="submit"></form></body>

</html>

Page 23: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

JavaScript Event: onkeydown<!DOCTYPE html><html><head><script>function myFunction() { alert("You pressed a key inside the input field");}</script></head><body>

<p>A function is triggered when the user is pressing a key in the input field.</p>

<input type="text" onkeydown="myFunction()">

</body></html>

Page 24: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

What is moreonblur - When a user leaves an input fieldonchange - When a user changes the content of an input fieldonchange - When a user selects a dropdown valueonfocus - When an input field gets focusonselect - When input text is selectedonsubmit - When a user clicks the submit buttononreset - When a user clicks the reset buttononkeydown - When a user is pressing/holding down a keyonkeypress - When a user is pressing/holding down a keyonkeyup - When the user releases a keyonkeyup - When the user releases a keyonkeydown vs onkeyup - Both

Page 25: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

Mouse Event<!DOCTYPE html><html><body>

<h1 onmouseover="style.color='red'" onmouseout="style.color='black'">Mouse over this text</h1>

</body></html>

Page 26: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

Mouse events<!DOCTYPE html><html><head><script>function myFunction(elmnt, clr) { elmnt.style.color = clr;}</script></head><body>

<p onmousedown="myFunction(this,'red')" onmouseup="myFunction(this,'green')">Click the text to change the color. A function, with parameters, is triggered when the mouse button is pressed down, and again, with other parameters, when the mouse button is released.</p>

</body></html>

Page 28: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

Practical• Please see practical 7 and follow the instruction

Page 29: JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }

Resources• w3schools.com