Webtechnology 1
Webtechnology 1
function functionName(parameters)
{
variable declaration;
Statement 1;
Statement 2;
……………….
Statement n;
return statement;
}
In branching statement, the selection is made based on condition. The execution of program
statements is totally directed by the result obtained from checking condition. The branching
statements are mainly of three types:
If…….else statement
If…….else if statement
switch case statement
<script>
function onFunction() {
alert ("Your browser is working online.");
}
function offFunction() {
alert ("Your browser is working offline.");
}
</script>
</body></html>
<script>
function myFunction() {
alert("This document is now being printed");
}
</script>
</body>
</html>
<script>
function myFunction() {
return "Are you sure you want to leave this page?";
}
</script>
</body>
</html>
<p>When you leave the input field, a function is triggered which transforms the input text to upper
case.</p>
<script>
function myFunction() {
let x = document.getElementById("fname");
x.value = x.value.toUpperCase();
}
</script>
</body>
</html>
<p id="demo"></p>
<script>
function myFunction() {
let x = document.getElementById("myInput").value;
document.getElementById("demo").innerHTML = "You wrote: " + x;
}
</script>
</body>
</html>
<p>If you click submit, without filling out the text field, an alert message will occur.</p>
</body>
</html>
<form onreset="myFunction()">
Enter name: <input type="text">
<input type="reset">
</form>
<script>
function myFunction() {
alert("The form was reset");
}
</script>
</body>
</html>
Prepared by: Govind Bhat
Event Handling
Example9:
<!DOCTYPE html>
<html>
<body>
<p>The function myFunction() is triggered when some text is selected in the input field. The function
shows a message.</p>
<script>
function myFunction() {
alert("You have selected some text!");
}
</script>
</body>
</html>
Prepared by: Govind Bhat
Event Handling
Example10:
<!DOCTYPE html>
<html>
<body>
<p>When you submit the form, a function is triggered which alerts some text.</p>
<script>
function myFunction() {
alert("The form was submitted");
}
</script>
</body>
</html>
Prepared by: Govind Bhat
Event Handling
Example11:
<!DOCTYPE html>
<html>
<body>
<p>A function is triggered when the user is pressing a key in the input field.</p>
<script>
function myFunction() {
alert("You pressed a key inside the input field");
}
</script>
</body>
</html>
<script>
function keydownFunction() {
document.getElementById("demo").style.backgroundColor = "red";
}
function keyupFunction() {
document.getElementById("demo").style.backgroundColor = "green";
}
</script>
</body></html>
Prepared by: Govind Bhat
Event Handling
Example13:
<!DOCTYPE html>
<html>
<body>
<script>
function myFunction() {
document.getElementById("demo").style.color = “red";
}
</script>
</body>
</html>
<script>
function myFunction() {
document.getElementById("demo").style.color = “green";
}
</script>
</body>
</html>