Professional Documents
Culture Documents
Web Technology: School of Computer Science and Applications Varish P V
Web Technology: School of Computer Science and Applications Varish P V
WEB TECHNOLOGY
Varish P V
Unit-4
UNIT 4
Load Unload
LOAD EVENT
<!DOCTYPE html>
<html>
<body onload="msg()">
<h1>Welcome to my Home Page</h1>
<p>Close this window or press F5 to reload the page.</p>
<p><strong>Note:</strong> Due to different browser settings, this event may not always
work as expected.</p>
<script>
function msg() {
alert(“Welcome to my home page");
}
</script>
</body>
</html>
Execute
HANDLING EVENTS FROM BUTTON ELEMENTS
CLICK EVENT
<!DOCTYPE html>
<html>
<head>
<script>
function msg() {
alert("Hello World");
}
</script>
</head>
<body>
<p>Click the button to trigger a function.</p>
<button onclick=“msg()"><img src="image/click.jpg" width="100" height="100" /></button>
</body>
</html>
Execute
CLICK EVENT
<!DOCTYPE html>
<html>
<head>
<script>
function color() {
document. body. style. background = "yellow";
}
</script>
</head>
<body>
<p>Click the button to change the page color</p>
<button onclick="color()">Change Color</button>
</body>
</html>
Execute
ONSUBMIT EVENT
<!DOCTYPE html>
<html>
<head>
<script>
function msg() {
alert("hello");
}
</script>
</head>
<body>
<p>Click submit to trigger a function.</p>
<form onsubmit="msg()">
<input type="submit">
</form>
</body>
</html>
Execute
SUMMARY
blur
focus
CHANGE EVENT
<!DOCTYPE html>
<html>
<body>
<p>Modify the text in the input field, then click outside the field to fire the onchange event.</p>
<script>
function chfun(val) {
alert("The input value has changed. The new value is: " + val);
}
</script>
</body>
</html>
Execute
CHANGE EVENT
<!DOCTYPE html>
<html>
<body>
Enter age: <input type="text" onchange="check(this.value)">
<script>
function check(age) {
if(age<18){
alert("Not eligible for voting");
}
else{
alert("Eligible for voting");
}
}
</script>
</body>
</html>
Execute
CHANGE EVENT
<html>
<body>
Enter Marks: <input type="text" onchange="check(this.value)">
<script>
function check(m) {
if(m<0){
alert("marks cannot be -ve");
}
else if(m>100){
alert("marks cannot be > 100");
}
else{
alert("done");
}
}
</script>
</body>
</html>
Execute
CHANGE EVENT
<html>
<body>
Enter Mobile No: <input type="text" onchange="check(this.value)">
<script>
function check(m) {
if(m.length == 10 ){
alert("correct");
}
else{
alert("incorrect number");
}
}
</script>
</body>
</html>
Execute
CHANGE EVENT
<!DOCTYPE html>
<html>
<body>
<form name="rate">
Enter product cost: <input type="text" name="pcost"> <br>
Enter gst rate: <input type="text" name="gst" onchange="calculate()"> <br>
Total Amount: <input type="text" name="tamt" >
</form>
<script>
function calculate() {
var c=parseFloat(document.rate.pcost.value);
var g=parseFloat(document.rate.gst.value);
document.rate.tamt.value=c+(g/100)*c;
}
</script>
</body>
</html>
Execute
DISCUSSION
KEY PRESS EVENT
<!DOCTYPE html>
<html>
<body>
<p>A function is triggered when the user is pressing a key in the input field.</p>
<script>
function myFunction() {
</script>
</body>
</html>
Execute
KEY UP & KEY DOWN
<!DOCTYPE html>
<html>
<body>
<p>Press and hold down a key inside the text field to set a red background color. Release the key
to set a green background color.</p>
<input type="text" id="demo" onkeydown="keydownFunction()" onkeyup="keyupFunction()">
<script>
function keydownFunction() {
document.getElementById("demo").style.backgroundColor = "red";
}
function keyupFunction() {
document.getElementById("demo").style.backgroundColor = "green";
}
</script>
</body>
</html>
Execute
KEY UP & KEY DOWN
<!DOCTYPE html>
<html>
<body>
Enter your name: <input type="text" id="fname" onkeyup="alp(this)" onkeydown="alp(this)">
<script>
function alp(ele){
var val = ele.value;
var len = val.length;
if(val.search(/[^a-zA-Z ]/) != -1){ // if(val.search(/\D/) != -1) --> for number check
alert("Enter Only Alphabets");
ele.value = ele.value.substring(0,len-1);
ele.select();
}
}
</script>
</body>
</html>
Execute
BLUR
<!DOCTYPE html>
<html>
<body>
<p>When you leave the input field, a function is triggered which transforms the input text to upper
case.</p>
<script>
function myFunction() {
var x = document.getElementById("fname");
x.value = x.value.toUpperCase();
}
</script>
</body>
</html>
FOCUS
<!DOCTYPE html>
<html>
<body>
<p>When the input field gets focus, a function is triggered which changes the background-
color.</p>
<script>
function myFunction(x) {
x.style.background = "yellow";
}
</script>
</body>
</html>
HANDLING EVENTS FROM PASSWORD ELEMENT
<html>
<body>
<form>
Password: <input type="password" id="myPsw">
</form>
<p>Write something in the password field, and then click the buttons to display and/or change the
value.</p>
<button onclick="display()">Display value</button>
<button onclick="change()">Change value</button>
<script>
function display() {
var x = document.getElementById("myPsw").value;
alert("The value of the password field is: " + x);
}
function change() {
var x = document.getElementById("myPsw").value = "NewPassword123";
alert ("The value was changed to: " + x);
}
</script>
</body>
</html>
DYNAMIC DOCUMENTS WITH JAVASCRIPT
• Scripts address the elements of the document using their DOM addresses
4. Web References :
www.w3schools.com
www.quora.com
www.coursera.org
Thank You