SlideShare a Scribd company logo
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>
Ad

Recommended

JavaScript Training
JavaScript Training
Ramindu Deshapriya
 
Practica n° 7
Practica n° 7
rafobarrientos
 
The Devil and HTML5
The Devil and HTML5
Myles Braithwaite
 
1cst
1cst
Griffinder VinHai
 
Html
Html
g Nama
 
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...
Nagios
 
2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript Security
Johannes Hoppe
 
Javascript
Javascript
Jitendra Negi
 
Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)
Remy Sharp
 
Angular js quickstart
Angular js quickstart
LinkMe Srl
 
Yearning jQuery
Yearning jQuery
Remy Sharp
 
Rushed to Victory Gardens' stage, An Issue of Blood is more effusion than play
Rushed to Victory Gardens' stage, An Issue of Blood is more effusion than play
chicagonewsyesterday
 
Makezine
Makezine
Kelly Thejitternews
 
Discontinuing Reader Matches
Discontinuing Reader Matches
chicagonewsonlineradio
 
Authentication
Authentication
soon
 
Rails, Postgres, Angular, and Bootstrap: The Power Stack
Rails, Postgres, Angular, and Bootstrap: The Power Stack
David Copeland
 
Odoo - CMS performances optimization
Odoo - CMS performances optimization
Odoo
 
Make More Money With Advanced Custom Fields - WordCampYYC 2015
Make More Money With Advanced Custom Fields - WordCampYYC 2015
buildstudio
 
Javascript 1
Javascript 1
pavishkumarsingh
 
Google
Google
soon
 
HTML5 & The Open Web - at Nackademin
HTML5 & The Open Web - at Nackademin
Robert Nyman
 
Introduction to AngularJS
Introduction to AngularJS
Marco Vito Moscaritolo
 
2013 05-03 - HTML5 & JavaScript Security
2013 05-03 - HTML5 & JavaScript Security
Johannes Hoppe
 
Ajax Performance Tuning and Best Practices
Ajax Performance Tuning and Best Practices
Doris Chen
 
Mad Max is back, plus the rest of our new reviews and notable screenings
Mad Max is back, plus the rest of our new reviews and notable screenings
chicagonewsonlineradio
 
Speed is a feature PyConAr 2014
Speed is a feature PyConAr 2014
Pablo Mouzo
 
Deploying
Deploying
soon
 
Javascript basics for automation testing
Javascript basics for automation testing
Vikas Thange
 
Java scipt
Java scipt
Ashish Gajjar Samvad Cell
 
Cordova training : Day 3 - Introduction to Javascript
Cordova training : Day 3 - Introduction to Javascript
Binu Paul
 

More Related Content

What's hot (19)

Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)
Remy Sharp
 
Angular js quickstart
Angular js quickstart
LinkMe Srl
 
Yearning jQuery
Yearning jQuery
Remy Sharp
 
Rushed to Victory Gardens' stage, An Issue of Blood is more effusion than play
Rushed to Victory Gardens' stage, An Issue of Blood is more effusion than play
chicagonewsyesterday
 
Makezine
Makezine
Kelly Thejitternews
 
Discontinuing Reader Matches
Discontinuing Reader Matches
chicagonewsonlineradio
 
Authentication
Authentication
soon
 
Rails, Postgres, Angular, and Bootstrap: The Power Stack
Rails, Postgres, Angular, and Bootstrap: The Power Stack
David Copeland
 
Odoo - CMS performances optimization
Odoo - CMS performances optimization
Odoo
 
Make More Money With Advanced Custom Fields - WordCampYYC 2015
Make More Money With Advanced Custom Fields - WordCampYYC 2015
buildstudio
 
Javascript 1
Javascript 1
pavishkumarsingh
 
Google
Google
soon
 
HTML5 & The Open Web - at Nackademin
HTML5 & The Open Web - at Nackademin
Robert Nyman
 
Introduction to AngularJS
Introduction to AngularJS
Marco Vito Moscaritolo
 
2013 05-03 - HTML5 & JavaScript Security
2013 05-03 - HTML5 & JavaScript Security
Johannes Hoppe
 
Ajax Performance Tuning and Best Practices
Ajax Performance Tuning and Best Practices
Doris Chen
 
Mad Max is back, plus the rest of our new reviews and notable screenings
Mad Max is back, plus the rest of our new reviews and notable screenings
chicagonewsonlineradio
 
Speed is a feature PyConAr 2014
Speed is a feature PyConAr 2014
Pablo Mouzo
 
Deploying
Deploying
soon
 
Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)
Remy Sharp
 
Angular js quickstart
Angular js quickstart
LinkMe Srl
 
Yearning jQuery
Yearning jQuery
Remy Sharp
 
Rushed to Victory Gardens' stage, An Issue of Blood is more effusion than play
Rushed to Victory Gardens' stage, An Issue of Blood is more effusion than play
chicagonewsyesterday
 
Authentication
Authentication
soon
 
Rails, Postgres, Angular, and Bootstrap: The Power Stack
Rails, Postgres, Angular, and Bootstrap: The Power Stack
David Copeland
 
Odoo - CMS performances optimization
Odoo - CMS performances optimization
Odoo
 
Make More Money With Advanced Custom Fields - WordCampYYC 2015
Make More Money With Advanced Custom Fields - WordCampYYC 2015
buildstudio
 
Google
Google
soon
 
HTML5 & The Open Web - at Nackademin
HTML5 & The Open Web - at Nackademin
Robert Nyman
 
2013 05-03 - HTML5 & JavaScript Security
2013 05-03 - HTML5 & JavaScript Security
Johannes Hoppe
 
Ajax Performance Tuning and Best Practices
Ajax Performance Tuning and Best Practices
Doris Chen
 
Mad Max is back, plus the rest of our new reviews and notable screenings
Mad Max is back, plus the rest of our new reviews and notable screenings
chicagonewsonlineradio
 
Speed is a feature PyConAr 2014
Speed is a feature PyConAr 2014
Pablo Mouzo
 
Deploying
Deploying
soon
 

Similar to Java script programms (20)

Javascript basics for automation testing
Javascript basics for automation testing
Vikas Thange
 
Java scipt
Java scipt
Ashish Gajjar Samvad Cell
 
Cordova training : Day 3 - Introduction to Javascript
Cordova training : Day 3 - Introduction to Javascript
Binu Paul
 
First javascript workshop : first basics
First javascript workshop : first basics
GhassenZrigua1
 
Java script
Java script
Soham Sengupta
 
Unit 4(it workshop)
Unit 4(it workshop)
Dr.Lokesh Gagnani
 
Javascript1
Javascript1
anas Mohtaseb
 
Js mod1
Js mod1
VARSHAKUMARI49
 
Java script
Java script
Fajar Baskoro
 
JavaScript Control Statements II
JavaScript Control Statements II
Reem Alattas
 
wp-UNIT_III.pptx
wp-UNIT_III.pptx
GANDHAMKUMAR2
 
Java Script basics and DOM
Java Script basics and DOM
Sukrit Gupta
 
Final Java-script.pptx
Final Java-script.pptx
AlkanthiSomesh
 
ppt 17 18.pptx
ppt 17 18.pptx
DrRavneetSingh
 
lect4
lect4
tutorialsruby
 
lect4
lect4
tutorialsruby
 
Training javascript 2012 hcmut
Training javascript 2012 hcmut
University of Technology
 
10. session 10 loops and arrays
10. session 10 loops and arrays
Phúc Đỗ
 
JavaScript
JavaScript
Rowena LI
 
Java Script
Java Script
Kalidass Balasubramaniam
 
Ad

More from Mukund Gandrakota (12)

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

Recently uploaded (20)

WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
ElysiumPro Company Profile 2025-2026.pdf
ElysiumPro Company Profile 2025-2026.pdf
info751436
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
machine learning is a advance technology
machine learning is a advance technology
ynancy893
 
60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
grade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quiz
norfapangolima
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
djiceramil
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
Low Power SI Class E Power Amplifier and Rf Switch for Health Care
Low Power SI Class E Power Amplifier and Rf Switch for Health Care
ieijjournal
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
ElysiumPro Company Profile 2025-2026.pdf
ElysiumPro Company Profile 2025-2026.pdf
info751436
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
machine learning is a advance technology
machine learning is a advance technology
ynancy893
 
60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
grade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quiz
norfapangolima
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
djiceramil
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
Low Power SI Class E Power Amplifier and Rf Switch for Health Care
Low Power SI Class E Power Amplifier and Rf Switch for Health Care
ieijjournal
 

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>