Java Script programs cs567
Simple timing
<!DOCTYPE html>
<html>
<head>
<script>
function timedText()
{
var x=document.getElementById('txt');
var t1=setTimeout(function(){x.value="2 seconds"},2000);
var t2=setTimeout(function(){x.value="4 seconds"},4000);
var t3=setTimeout(function(){x.value="6 seconds"},6000);
}
</script>
</head>
<body>
<form>
<input type="button" value="Display timed text!" onClick="timedText()" />
<input type="text" id="txt" />
</form>
<p>Click on the button above. The input field will tell you when two, four, and six seconds have
passed.</p>
</body>
</html>
Java Script programs cs567
Timing event in an infinite loop - with a Stop button
<!DOCTYPE html>
<html>
<head>
<script>
var c=0;
var t;
var timer_is_on=0;
function timedCount()
{
document.getElementById('txt').value=c;
c=c+1;
t=setTimeout(function(){timedCount()},1000);
}
function doTimer()
{
if (!timer_is_on)
{
timer_is_on=1;
timedCount();
}
}
function stopCount()
{
clearTimeout(t);
Java Script programs cs567
timer_is_on=0;
}
</script>
</head>
<body>
<form>
<input type="button" value="Start count!" onClick="doTimer()" />
<input type="text" id="txt" />
<input type="button" value="Stop count!" onClick="stopCount()" />
</form>
<p>
Click on the "Start count!" button above to start the timer. The input field will count forever,
starting at 0. Click on the "Stop count!" button to stop the counting. Click on the "Start count!"
button to start the timer again.
</p>
</body>
</html>
A clock created with a timing event
<!DOCTYPE html>
<html>
<head>
<script>
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
Java Script programs cs567
var s=today.getSeconds();
// add a zero in front of numbers<10<br>
m=checkTime(m);
s=checkTime(s);
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
t=setTimeout(function(){startTime()},500);
}
function checkTime(i)
{
if (i<10)
{
i="0" + i;
}
return i;
}
</script>
</head>
<body onload="startTime()">
<div id="txt">< /div>
</body>
</html>
Write to the Document with JavaScript
<!DOCTYPE html>
<html>
<body>
Java Script programs cs567
<h3>My First Web Page</h3>
<script>
document.write("<p>My First JavaScript</p>");
</script>
</body>
</html>
Change HTML elements with JavaScript
<!DOCTYPE html>
<html >
<body >
<h3>My First Web Page</h3>
<p id="demo">My First Paragraph.</p>
<script>
document.getElementById("demo").innerHTML="My First JavaScript";
</script>
</body>
</html>
Java Script programs cs567
< !DOCTYPE html>
< html>
< body>
< h3>My Web Page
< p id="demo">A Paragraph.
< button type="button" onclick="myFunction()">Try it
< p>< strong>Note: myFunction is stored in an external file called "myScript.js".
< script type="text/javascript" src="myScript.js">
< /body>
< /html>
If statement
<!DOCTYPE html>
<html>
<body>
<p>Click the button to get a "Good day" greeting if the time is less than 20:00.</p>
<button onClick="myFunction()">Try it</button>
<p id="demo">< /p>
Java Script programs cs567
<script>
function myFunction()
{
var x="";
var time=new Date().getHours();v
if (time<20)
{
&nbsp;&nbsp;x="Good day";
}
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>
If...else statement
<!DOCTYPE html>
<html>
<body>
<p>Click the button to get a time-based greeting.</p>
<button onClick="myFunction()">Try it</button>
<p id="demo">< /p>
Java Script programs cs567
<script>
function myFunction()
{
var x="";
var time=new Date().getHours();
if (time<20)
{
x="Good day";
}
else
{
x="Good evening";
}
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>
Switch statement
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display what day it is today.</p>
<button onClick="myFunction()">Try it</button>
Java Script programs cs567
<p id="demo">< /p>
<script>
function myFunction()
{
var x;
var d=new Date().getDay();
switch (d)
{
case 0:
x="Today it's Sunday";
break;
case 1:
x="Today it's Monday";
break;
case 2:
x="Today it's Tuesday";
break;
case 3:
x="Today it's Wednesday";
break;
case 4:
x="Today it's Thursday";
break;
case 5:
x="Today it's Friday";
break;
case 6:
Java Script programs cs567
x="Today it's Saturday";
break;
}
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>
The try...catch statement
<!DOCTYPE html>
<html>
<head>
<script>
var txt="";
function message()
{
try
{
adddlert("Welcome guest!");
}
catch(err)
{
txt="There was an error on this page.nn";
txt+="Error description: " + err.message + "nn";v
txt+="Click OK to continue.nn";
alert(txt);
Java Script programs cs567
}
}
</script>
</head>
<body>
<input type="button" value="View message" onClick="message()" />
</body>
</html>
The try...catch statement with a confirm box
<!DOCTYPE html>
<html>
<head>
<script>
var txt="";
function message()
{
try
{
adddlert("Welcome guest!");
}
catch(err)
{
txt="There was an error on this page.nn";
txt+="Click OK to continue viewing this page,n";v
txt+="or Cancel to return to the home page.nn";
if(!confirm(txt))
Java Script programs cs567
{
document.location.href="http://www.google.com/";
}
}
}
</script>
</head>
<body>
<input type="button" value="View message" onClick="message()" />
</body>
</html>
Acting to the onclick event
<!DOCTYPE html>
<html>
<head>
<script>
function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
</script>
</head>
<body>
<h1>My First JavaScript< /h1>
Java Script programs cs567
<p id="demo">This is a paragraph.< /p>
<button type="button" onclick="displayDate()">Display Date< /button>
</body>
</html>
Call a function
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
alert("Hello World!");
}
</script>
</head>
<body>
<button onClick="myFunction()">Try it</button>
<p>By clicking the button above, a function will be called. The function will alert a message.</p>
</body>
</html>
Java Script programs cs567
Function with an argument
<!DOCTYPE html>
<html>
<body>
<p>Click the button to call a function with arguments</p>
<button onClick="myFunction('Harry Potter','Wizard')">Try it</button>
<script>
function myFunction(name,job)
{
alert("Welcome " + name + ", the " + job);
}
</script>
</body>
</html>
Function with an argument 2
<!DOCTYPE html>
<html>
<head>
<script>
function myfunction(txt)
{
alert(txt);
Java Script programs cs567
}
</script>
</head>
<body>
<form>
<input type="button"
onclick="myfunction('Good Morning!')"
value="In the Morning">
<input type="button"
onclick="myfunction('Good Evening!')"
value="In the Evening">
</form>
<p>
When you click on one of the buttons, a function will be called. The function will alert
the argument that is passed to it.
</p>
</body>
</html>
Function that returns a value
<!DOCTYPE html>
<html>
<head>
<script>
Java Script programs cs567
function myFunction()
{
return ("Hello world!");
}
</script>
</head>
<body>
<script>
document.write(myFunction())
</script>
</body>
</html>
Function with arguments, that returns a value
<!DOCTYPE html>
<html>
<body>
<p>This example calls a function which perfoms a calculation, and returns the result:</p>
<p id="demo"></p>
<script>
function myFunction(a,b)
{
return a*b;
Java Script programs cs567
}
document.getElementById("demo").innerHTML=myFunction(4,3);
</script>
</body>
</html>
DEMO
< head >
< script >
function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
< /head >
< body >
< h3>My First JavaScript< / h3 >
< p id="demo">This is a paragraph.< /p >
< button type="button" onclick="displayDate()">Display Date< / button >
< / body >
For loop
<!DOCTYPE html>
<html>
<body>
<p>Click the button to loop through a block of code five times.< /p>
Java Script programs cs567
<button onclick="myFunction()">Try it< /button>
<p id="demo">< /p>
<script>
function myFunction()
{
var x="",i;
for (i=0;i<5;i++)
{
x=x + "The number is " + i + "< br>";
}
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>
Looping through HTML headers
<!DOCTYPE html>
<html>
<body>
<p>Click the button to loop from 1 to 6, to make HTML headings.< /p>
<button onclick="myFunction()">Try it< /button>
<div id="demo">< /div>
Java Script programs cs567
<script>
function myFunction()
{
var x="",i;
for (i=1; i<=6; i++)
{
x=x + "< h" + i + ">Heading " + i + "< / h" + i + ">";
}
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>
While loop
<!DOCTYPE html>
<html>
<body>
<p>Click the button to loop through a block of as long as < em>i< /em> is less than 5.< /p>
<button onclick="myFunction()">Try it< /button>
<p id="demo">< /p>
<script>
function myFunction()
{
var x="",i=0;
Java Script programs cs567
while (i<5)
{
x=x + "The number is " + i + "< br>";
i++;
}
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>
Do While loop
<!DOCTYPE html>
<html>
<body>
<p>Click the button to loop through a block of as long as < em>i< /em> is less than 5.< /p>
<button onclick="myFunction()">Try it< /button>
<p id="demo">< /p>
<script>
function myFunction()
{
var x="",i=0;
do
{
x=x + "The number is " + i + "< br>";
Java Script programs cs567
i++;
}
while (i<5)
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>
Break a loop
<!DOCTYPE html>
<html>
<body>
<p>Click the button to do a loop with a break.< /p>
<button onclick="myFunction()">Try it< /button>
<p id="demo">< /p>
<script>
function myFunction()
{
var x="",i=0;
for (i=0;i<10;i++)
{
if (i==3)
{
break;v
Java Script programs cs567
}
x=x + "The number is " + i + "< br>";
}
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>
Break and continue a loop
<p>Click the button to do a loop which will skip the step where i=3.</p>
<button onClick="myFunction()">Try it</button>
<p id="demo">< /p>
<script>
function myFunction()
{
var x="",i=0;
for (i=0;i<10;i++)
{
if (i==3)
{
continue;
}
x=x + "The number is " + i + "< br>";
}
document.getElementById("demo").innerHTML=x;
Java Script programs cs567
}
</script>
</body>
</html>
Alert box
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
alert("Hello! I am an alert box!");
}
</script>
</head>
< body>
<input type="button" onClick="myFunction()" value="Show alert box" />
</body>
</html>
Confirm box
<p>Click the button to display a confirm box.</p>
Java Script programs cs567
<button onClick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction()
{
var x;
var r=confirm("Press a button!");
if (r==true)
{
x="You pressed OK!";
}
else
{
x="You pressed Cancel!";
}
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>
Prompt box
<!DOCTYPE html>
<html>
<body>
Java Script programs cs567
<p>Click the button to demonstrate the prompt box.</p>
<button onClick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction()
{
var x;
var name=prompt("Please enter your name","Harry Potter");
if (name!=null)
{
x="Hello " + name + "! How are you today?";
document.getElementById("demo").innerHTML=x;
}
}
</script>
</body>
</html>
JavaScript statements
<!DOCTYPE html>
<html>
Java Script programs cs567
<body>
<h1>My Web Page</h1>
<p id="demo">A Paragraph.</p>
<div id="myDIV">A DIV.</div>
<script>
document.getElementById("demo").innerHTML="Hello Dolly";
document.getElementById("myDIV").innerHTML="How are you?";
</script>
</body>
</html>
JavaScript blocks
<!DOCTYPE html>
<html>
<body>
<h1>My Web Page</h1>
<p id="myPar">I am a paragraph.</p>
<div id="myDiv">I am a div.</div>
<p>
<button type="button" onClick="myFunction()">Try it</button>
Java Script programs cs567
</p>
<script>
function myFunction()
{
document.getElementById("myPar").innerHTML="Hello Dolly";
document.getElementById("myDiv").innerHTML="How are you?";
}
</script>
<p>When you click on "Try it", the two elements will change.</p>
</body>
</html>
Single line comments
<!DOCTYPE html>
<html>
<body>
<h1 id="myH1"></h1>
<p id="myP"></p>
<script>
Java Script programs cs567
// Write to a heading:
document.getElementById("myH1").innerHTML="Welcome to my Homepage";
// Write to a paragraph:
document.getElementById("myP").innerHTML="This is my first paragraph.";
</script>
<p>< strong>Note:</ strong> The comments are not executed.</p>
</body>
</html>
Declare a variable, assign a value to it, and display it
<!DOCTYPE html>
<html>
<body>
<script>
var firstname;
firstname="Hege";
document.write(firstname);
document.write("");
firstname="Tove";
document.write(firstname);
</script>
<p>The script above declares a variable,
assigns a value to it, displays the value, changes the value,
and displays the value again.< /p>
Java Script programs cs567
</body>
</html>

More Related Content

PPT
JavaScript Training
PDF
Practica n° 7
KEY
The Devil and HTML5
TXT
Html
ODP
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...
PDF
2013-06-25 - HTML5 & JavaScript Security
DOCX
Javascript
JavaScript Training
Practica n° 7
The Devil and HTML5
Html
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...
2013-06-25 - HTML5 & JavaScript Security
Javascript

What's hot (19)

PDF
Is HTML5 Ready? (workshop)
PDF
Angular js quickstart
PDF
Yearning jQuery
PDF
Rushed to Victory Gardens' stage, An Issue of Blood is more effusion than play
TXT
PDF
Discontinuing Reader Matches
KEY
Authentication
PPTX
Rails, Postgres, Angular, and Bootstrap: The Power Stack
PDF
Odoo - CMS performances optimization
PDF
Make More Money With Advanced Custom Fields - WordCampYYC 2015
PPTX
Javascript 1
KEY
Google
PDF
HTML5 & The Open Web - at Nackademin
PDF
Introduction to AngularJS
PDF
2013 05-03 - HTML5 & JavaScript Security
PDF
Ajax Performance Tuning and Best Practices
PDF
Mad Max is back, plus the rest of our new reviews and notable screenings
PDF
Speed is a feature PyConAr 2014
KEY
Deploying
Is HTML5 Ready? (workshop)
Angular js quickstart
Yearning jQuery
Rushed to Victory Gardens' stage, An Issue of Blood is more effusion than play
Discontinuing Reader Matches
Authentication
Rails, Postgres, Angular, and Bootstrap: The Power Stack
Odoo - CMS performances optimization
Make More Money With Advanced Custom Fields - WordCampYYC 2015
Javascript 1
Google
HTML5 & The Open Web - at Nackademin
Introduction to AngularJS
2013 05-03 - HTML5 & JavaScript Security
Ajax Performance Tuning and Best Practices
Mad Max is back, plus the rest of our new reviews and notable screenings
Speed is a feature PyConAr 2014
Deploying
Ad

Similar to Java script programms (20)

PPTX
Javascript basics for automation testing
PPTX
Cordova training : Day 3 - Introduction to Javascript
PPTX
First javascript workshop : first basics
PPT
Java script
PDF
Unit 4(it workshop)
PPT
Javascript1
PPT
PPT
Java script
PPT
JavaScript Control Statements II
PPTX
wp-UNIT_III.pptx
PPTX
Java Script basics and DOM
PPTX
Final Java-script.pptx
PPTX
ppt 17 18.pptx
PDF
PDF
PDF
Training javascript 2012 hcmut
PPTX
10. session 10 loops and arrays
PPT
JavaScript
PPTX
Javascript basics for automation testing
Cordova training : Day 3 - Introduction to Javascript
First javascript workshop : first basics
Java script
Unit 4(it workshop)
Javascript1
Java script
JavaScript Control Statements II
wp-UNIT_III.pptx
Java Script basics and DOM
Final Java-script.pptx
ppt 17 18.pptx
Training javascript 2012 hcmut
10. session 10 loops and arrays
JavaScript
Ad

More from Mukund Gandrakota (12)

PDF
Edc unit 8
PDF
Edc unit 7
PDF
Edc unit 6
PDF
Edc unit 5
PDF
Edc unit 4
PDF
Edc unit 3
PDF
Edc unit 2
PDF
Edc unit 1
PDF
Java programs
PDF
C++ programs
PDF
C programms
PDF
career after graduated in cse
Edc unit 8
Edc unit 7
Edc unit 6
Edc unit 5
Edc unit 4
Edc unit 3
Edc unit 2
Edc unit 1
Java programs
C++ programs
C programms
career after graduated in cse

Recently uploaded (20)

PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PDF
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
PPTX
ASME PCC-02 TRAINING -DESKTOP-NLE5HNP.pptx
PDF
Computer organization and architecuture Digital Notes....pdf
PDF
Unit1 - AIML Chapter 1 concept and ethics
PDF
Beginners-Guide-to-Artificial-Intelligence.pdf
PDF
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
PDF
Applications of Equal_Area_Criterion.pdf
PDF
Cryptography and Network Security-Module-I.pdf
PDF
distributed database system" (DDBS) is often used to refer to both the distri...
PPTX
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
PDF
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
DOC
T Pandian CV Madurai pandi kokkaf illaya
PDF
20250617 - IR - Global Guide for HR - 51 pages.pdf
PDF
August -2025_Top10 Read_Articles_ijait.pdf
PPTX
Module 8- Technological and Communication Skills.pptx
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PPTX
Amdahl’s law is explained in the above power point presentations
PDF
MLpara ingenieira CIVIL, meca Y AMBIENTAL
PPTX
MAD Unit - 3 User Interface and Data Management (Diploma IT)
"Array and Linked List in Data Structures with Types, Operations, Implementat...
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
ASME PCC-02 TRAINING -DESKTOP-NLE5HNP.pptx
Computer organization and architecuture Digital Notes....pdf
Unit1 - AIML Chapter 1 concept and ethics
Beginners-Guide-to-Artificial-Intelligence.pdf
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
Applications of Equal_Area_Criterion.pdf
Cryptography and Network Security-Module-I.pdf
distributed database system" (DDBS) is often used to refer to both the distri...
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
T Pandian CV Madurai pandi kokkaf illaya
20250617 - IR - Global Guide for HR - 51 pages.pdf
August -2025_Top10 Read_Articles_ijait.pdf
Module 8- Technological and Communication Skills.pptx
Exploratory_Data_Analysis_Fundamentals.pdf
Amdahl’s law is explained in the above power point presentations
MLpara ingenieira CIVIL, meca Y AMBIENTAL
MAD Unit - 3 User Interface and Data Management (Diploma IT)

Java script programms

  • 1. Java Script programs cs567 Simple timing <!DOCTYPE html> <html> <head> <script> function timedText() { var x=document.getElementById('txt'); var t1=setTimeout(function(){x.value="2 seconds"},2000); var t2=setTimeout(function(){x.value="4 seconds"},4000); var t3=setTimeout(function(){x.value="6 seconds"},6000); } </script> </head> <body> <form> <input type="button" value="Display timed text!" onClick="timedText()" /> <input type="text" id="txt" /> </form> <p>Click on the button above. The input field will tell you when two, four, and six seconds have passed.</p> </body> </html>
  • 2. Java Script programs cs567 Timing event in an infinite loop - with a Stop button <!DOCTYPE html> <html> <head> <script> var c=0; var t; var timer_is_on=0; function timedCount() { document.getElementById('txt').value=c; c=c+1; t=setTimeout(function(){timedCount()},1000); } function doTimer() { if (!timer_is_on) { timer_is_on=1; timedCount(); } } function stopCount() { clearTimeout(t);
  • 3. Java Script programs cs567 timer_is_on=0; } </script> </head> <body> <form> <input type="button" value="Start count!" onClick="doTimer()" /> <input type="text" id="txt" /> <input type="button" value="Stop count!" onClick="stopCount()" /> </form> <p> Click on the "Start count!" button above to start the timer. The input field will count forever, starting at 0. Click on the "Stop count!" button to stop the counting. Click on the "Start count!" button to start the timer again. </p> </body> </html> A clock created with a timing event <!DOCTYPE html> <html> <head> <script> function startTime() { var today=new Date(); var h=today.getHours(); var m=today.getMinutes();
  • 4. Java Script programs cs567 var s=today.getSeconds(); // add a zero in front of numbers<10<br> m=checkTime(m); s=checkTime(s); document.getElementById('txt').innerHTML=h+":"+m+":"+s; t=setTimeout(function(){startTime()},500); } function checkTime(i) { if (i<10) { i="0" + i; } return i; } </script> </head> <body onload="startTime()"> <div id="txt">< /div> </body> </html> Write to the Document with JavaScript <!DOCTYPE html> <html> <body>
  • 5. Java Script programs cs567 <h3>My First Web Page</h3> <script> document.write("<p>My First JavaScript</p>"); </script> </body> </html> Change HTML elements with JavaScript <!DOCTYPE html> <html > <body > <h3>My First Web Page</h3> <p id="demo">My First Paragraph.</p> <script> document.getElementById("demo").innerHTML="My First JavaScript"; </script> </body> </html>
  • 6. Java Script programs cs567 < !DOCTYPE html> < html> < body> < h3>My Web Page < p id="demo">A Paragraph. < button type="button" onclick="myFunction()">Try it < p>< strong>Note: myFunction is stored in an external file called "myScript.js". < script type="text/javascript" src="myScript.js"> < /body> < /html> If statement <!DOCTYPE html> <html> <body> <p>Click the button to get a "Good day" greeting if the time is less than 20:00.</p> <button onClick="myFunction()">Try it</button> <p id="demo">< /p>
  • 7. Java Script programs cs567 <script> function myFunction() { var x=""; var time=new Date().getHours();v if (time<20) { &nbsp;&nbsp;x="Good day"; } document.getElementById("demo").innerHTML=x; } </script> </body> </html> If...else statement <!DOCTYPE html> <html> <body> <p>Click the button to get a time-based greeting.</p> <button onClick="myFunction()">Try it</button> <p id="demo">< /p>
  • 8. Java Script programs cs567 <script> function myFunction() { var x=""; var time=new Date().getHours(); if (time<20) { x="Good day"; } else { x="Good evening"; } document.getElementById("demo").innerHTML=x; } </script> </body> </html> Switch statement <!DOCTYPE html> <html> <body> <p>Click the button to display what day it is today.</p> <button onClick="myFunction()">Try it</button>
  • 9. Java Script programs cs567 <p id="demo">< /p> <script> function myFunction() { var x; var d=new Date().getDay(); switch (d) { case 0: x="Today it's Sunday"; break; case 1: x="Today it's Monday"; break; case 2: x="Today it's Tuesday"; break; case 3: x="Today it's Wednesday"; break; case 4: x="Today it's Thursday"; break; case 5: x="Today it's Friday"; break; case 6:
  • 10. Java Script programs cs567 x="Today it's Saturday"; break; } document.getElementById("demo").innerHTML=x; } </script> </body> </html> The try...catch statement <!DOCTYPE html> <html> <head> <script> var txt=""; function message() { try { adddlert("Welcome guest!"); } catch(err) { txt="There was an error on this page.nn"; txt+="Error description: " + err.message + "nn";v txt+="Click OK to continue.nn"; alert(txt);
  • 11. Java Script programs cs567 } } </script> </head> <body> <input type="button" value="View message" onClick="message()" /> </body> </html> The try...catch statement with a confirm box <!DOCTYPE html> <html> <head> <script> var txt=""; function message() { try { adddlert("Welcome guest!"); } catch(err) { txt="There was an error on this page.nn"; txt+="Click OK to continue viewing this page,n";v txt+="or Cancel to return to the home page.nn"; if(!confirm(txt))
  • 12. Java Script programs cs567 { document.location.href="http://www.google.com/"; } } } </script> </head> <body> <input type="button" value="View message" onClick="message()" /> </body> </html> Acting to the onclick event <!DOCTYPE html> <html> <head> <script> function displayDate() { document.getElementById("demo").innerHTML=Date(); } </script> </head> <body> <h1>My First JavaScript< /h1>
  • 13. Java Script programs cs567 <p id="demo">This is a paragraph.< /p> <button type="button" onclick="displayDate()">Display Date< /button> </body> </html> Call a function <!DOCTYPE html> <html> <head> <script> function myFunction() { alert("Hello World!"); } </script> </head> <body> <button onClick="myFunction()">Try it</button> <p>By clicking the button above, a function will be called. The function will alert a message.</p> </body> </html>
  • 14. Java Script programs cs567 Function with an argument <!DOCTYPE html> <html> <body> <p>Click the button to call a function with arguments</p> <button onClick="myFunction('Harry Potter','Wizard')">Try it</button> <script> function myFunction(name,job) { alert("Welcome " + name + ", the " + job); } </script> </body> </html> Function with an argument 2 <!DOCTYPE html> <html> <head> <script> function myfunction(txt) { alert(txt);
  • 15. Java Script programs cs567 } </script> </head> <body> <form> <input type="button" onclick="myfunction('Good Morning!')" value="In the Morning"> <input type="button" onclick="myfunction('Good Evening!')" value="In the Evening"> </form> <p> When you click on one of the buttons, a function will be called. The function will alert the argument that is passed to it. </p> </body> </html> Function that returns a value <!DOCTYPE html> <html> <head> <script>
  • 16. Java Script programs cs567 function myFunction() { return ("Hello world!"); } </script> </head> <body> <script> document.write(myFunction()) </script> </body> </html> Function with arguments, that returns a value <!DOCTYPE html> <html> <body> <p>This example calls a function which perfoms a calculation, and returns the result:</p> <p id="demo"></p> <script> function myFunction(a,b) { return a*b;
  • 17. Java Script programs cs567 } document.getElementById("demo").innerHTML=myFunction(4,3); </script> </body> </html> DEMO < head > < script > function displayDate() { document.getElementById("demo").innerHTML=Date(); } < /head > < body > < h3>My First JavaScript< / h3 > < p id="demo">This is a paragraph.< /p > < button type="button" onclick="displayDate()">Display Date< / button > < / body > For loop <!DOCTYPE html> <html> <body> <p>Click the button to loop through a block of code five times.< /p>
  • 18. Java Script programs cs567 <button onclick="myFunction()">Try it< /button> <p id="demo">< /p> <script> function myFunction() { var x="",i; for (i=0;i<5;i++) { x=x + "The number is " + i + "< br>"; } document.getElementById("demo").innerHTML=x; } </script> </body> </html> Looping through HTML headers <!DOCTYPE html> <html> <body> <p>Click the button to loop from 1 to 6, to make HTML headings.< /p> <button onclick="myFunction()">Try it< /button> <div id="demo">< /div>
  • 19. Java Script programs cs567 <script> function myFunction() { var x="",i; for (i=1; i<=6; i++) { x=x + "< h" + i + ">Heading " + i + "< / h" + i + ">"; } document.getElementById("demo").innerHTML=x; } </script> </body> </html> While loop <!DOCTYPE html> <html> <body> <p>Click the button to loop through a block of as long as < em>i< /em> is less than 5.< /p> <button onclick="myFunction()">Try it< /button> <p id="demo">< /p> <script> function myFunction() { var x="",i=0;
  • 20. Java Script programs cs567 while (i<5) { x=x + "The number is " + i + "< br>"; i++; } document.getElementById("demo").innerHTML=x; } </script> </body> </html> Do While loop <!DOCTYPE html> <html> <body> <p>Click the button to loop through a block of as long as < em>i< /em> is less than 5.< /p> <button onclick="myFunction()">Try it< /button> <p id="demo">< /p> <script> function myFunction() { var x="",i=0; do { x=x + "The number is " + i + "< br>";
  • 21. Java Script programs cs567 i++; } while (i<5) document.getElementById("demo").innerHTML=x; } </script> </body> </html> Break a loop <!DOCTYPE html> <html> <body> <p>Click the button to do a loop with a break.< /p> <button onclick="myFunction()">Try it< /button> <p id="demo">< /p> <script> function myFunction() { var x="",i=0; for (i=0;i<10;i++) { if (i==3) { break;v
  • 22. Java Script programs cs567 } x=x + "The number is " + i + "< br>"; } document.getElementById("demo").innerHTML=x; } </script> </body> </html> Break and continue a loop <p>Click the button to do a loop which will skip the step where i=3.</p> <button onClick="myFunction()">Try it</button> <p id="demo">< /p> <script> function myFunction() { var x="",i=0; for (i=0;i<10;i++) { if (i==3) { continue; } x=x + "The number is " + i + "< br>"; } document.getElementById("demo").innerHTML=x;
  • 23. Java Script programs cs567 } </script> </body> </html> Alert box <!DOCTYPE html> <html> <head> <script> function myFunction() { alert("Hello! I am an alert box!"); } </script> </head> < body> <input type="button" onClick="myFunction()" value="Show alert box" /> </body> </html> Confirm box <p>Click the button to display a confirm box.</p>
  • 24. Java Script programs cs567 <button onClick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var x; var r=confirm("Press a button!"); if (r==true) { x="You pressed OK!"; } else { x="You pressed Cancel!"; } document.getElementById("demo").innerHTML=x; } </script> </body> </html> Prompt box <!DOCTYPE html> <html> <body>
  • 25. Java Script programs cs567 <p>Click the button to demonstrate the prompt box.</p> <button onClick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var x; var name=prompt("Please enter your name","Harry Potter"); if (name!=null) { x="Hello " + name + "! How are you today?"; document.getElementById("demo").innerHTML=x; } } </script> </body> </html> JavaScript statements <!DOCTYPE html> <html>
  • 26. Java Script programs cs567 <body> <h1>My Web Page</h1> <p id="demo">A Paragraph.</p> <div id="myDIV">A DIV.</div> <script> document.getElementById("demo").innerHTML="Hello Dolly"; document.getElementById("myDIV").innerHTML="How are you?"; </script> </body> </html> JavaScript blocks <!DOCTYPE html> <html> <body> <h1>My Web Page</h1> <p id="myPar">I am a paragraph.</p> <div id="myDiv">I am a div.</div> <p> <button type="button" onClick="myFunction()">Try it</button>
  • 27. Java Script programs cs567 </p> <script> function myFunction() { document.getElementById("myPar").innerHTML="Hello Dolly"; document.getElementById("myDiv").innerHTML="How are you?"; } </script> <p>When you click on "Try it", the two elements will change.</p> </body> </html> Single line comments <!DOCTYPE html> <html> <body> <h1 id="myH1"></h1> <p id="myP"></p> <script>
  • 28. Java Script programs cs567 // Write to a heading: document.getElementById("myH1").innerHTML="Welcome to my Homepage"; // Write to a paragraph: document.getElementById("myP").innerHTML="This is my first paragraph."; </script> <p>< strong>Note:</ strong> The comments are not executed.</p> </body> </html> Declare a variable, assign a value to it, and display it <!DOCTYPE html> <html> <body> <script> var firstname; firstname="Hege"; document.write(firstname); document.write(""); firstname="Tove"; document.write(firstname); </script> <p>The script above declares a variable, assigns a value to it, displays the value, changes the value, and displays the value again.< /p>
  • 29. Java Script programs cs567 </body> </html>