SlideShare a Scribd company logo
1 of 29
Download to read offline
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

What's hot

Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)Remy Sharp
 
Angular js quickstart
Angular js quickstartAngular js quickstart
Angular js quickstartLinkMe Srl
 
Yearning jQuery
Yearning jQueryYearning jQuery
Yearning jQueryRemy 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 playRushed 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 playchicagonewsyesterday
 
Authentication
AuthenticationAuthentication
Authenticationsoon
 
Rails, Postgres, Angular, and Bootstrap: The Power Stack
Rails, Postgres, Angular, and Bootstrap: The Power StackRails, Postgres, Angular, and Bootstrap: The Power Stack
Rails, Postgres, Angular, and Bootstrap: The Power StackDavid Copeland
 
Odoo - CMS performances optimization
Odoo - CMS performances optimizationOdoo - CMS performances optimization
Odoo - CMS performances optimizationOdoo
 
Make More Money With Advanced Custom Fields - WordCampYYC 2015
Make More Money With Advanced Custom Fields - WordCampYYC 2015Make More Money With Advanced Custom Fields - WordCampYYC 2015
Make More Money With Advanced Custom Fields - WordCampYYC 2015buildstudio
 
Google
GoogleGoogle
Googlesoon
 
HTML5 & The Open Web - at Nackademin
HTML5 & The Open Web -  at NackademinHTML5 & The Open Web -  at Nackademin
HTML5 & The Open Web - at NackademinRobert Nyman
 
2013 05-03 - HTML5 & JavaScript Security
2013 05-03 -  HTML5 & JavaScript Security2013 05-03 -  HTML5 & JavaScript Security
2013 05-03 - HTML5 & JavaScript SecurityJohannes Hoppe
 
Ajax Performance Tuning and Best Practices
Ajax Performance Tuning and Best PracticesAjax Performance Tuning and Best Practices
Ajax Performance Tuning and Best PracticesDoris 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 screeningsMad 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 screeningschicagonewsonlineradio
 
Speed is a feature PyConAr 2014
Speed is a feature PyConAr 2014Speed is a feature PyConAr 2014
Speed is a feature PyConAr 2014Pablo Mouzo
 
Deploying
DeployingDeploying
Deployingsoon
 

What's hot (19)

Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)
 
Angular js quickstart
Angular js quickstartAngular js quickstart
Angular js quickstart
 
Yearning jQuery
Yearning jQueryYearning jQuery
Yearning jQuery
 
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 playRushed 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
 
Makezine
MakezineMakezine
Makezine
 
Discontinuing Reader Matches
Discontinuing Reader MatchesDiscontinuing Reader Matches
Discontinuing Reader Matches
 
Authentication
AuthenticationAuthentication
Authentication
 
Rails, Postgres, Angular, and Bootstrap: The Power Stack
Rails, Postgres, Angular, and Bootstrap: The Power StackRails, Postgres, Angular, and Bootstrap: The Power Stack
Rails, Postgres, Angular, and Bootstrap: The Power Stack
 
Odoo - CMS performances optimization
Odoo - CMS performances optimizationOdoo - CMS performances optimization
Odoo - CMS performances optimization
 
Make More Money With Advanced Custom Fields - WordCampYYC 2015
Make More Money With Advanced Custom Fields - WordCampYYC 2015Make More Money With Advanced Custom Fields - WordCampYYC 2015
Make More Money With Advanced Custom Fields - WordCampYYC 2015
 
Javascript 1
Javascript 1Javascript 1
Javascript 1
 
Google
GoogleGoogle
Google
 
HTML5 & The Open Web - at Nackademin
HTML5 & The Open Web -  at NackademinHTML5 & The Open Web -  at Nackademin
HTML5 & The Open Web - at Nackademin
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
2013 05-03 - HTML5 & JavaScript Security
2013 05-03 -  HTML5 & JavaScript Security2013 05-03 -  HTML5 & JavaScript Security
2013 05-03 - HTML5 & JavaScript Security
 
Ajax Performance Tuning and Best Practices
Ajax Performance Tuning and Best PracticesAjax Performance Tuning and Best Practices
Ajax Performance Tuning and Best Practices
 
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 screeningsMad 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
 
Speed is a feature PyConAr 2014
Speed is a feature PyConAr 2014Speed is a feature PyConAr 2014
Speed is a feature PyConAr 2014
 
Deploying
DeployingDeploying
Deploying
 

Similar to Java script programms (20)

JavaScript Operators
JavaScript OperatorsJavaScript Operators
JavaScript Operators
 
Javascript basic programs
Javascript basic programsJavascript basic programs
Javascript basic programs
 
lect4
lect4lect4
lect4
 
lect4
lect4lect4
lect4
 
Java script events
Java script  eventsJava script  events
Java script events
 
14922 java script built (1)
14922 java script built (1)14922 java script built (1)
14922 java script built (1)
 
Java script errors &amp; exceptions handling
Java script  errors &amp; exceptions handlingJava script  errors &amp; exceptions handling
Java script errors &amp; exceptions handling
 
計算機概論20161212
計算機概論20161212計算機概論20161212
計算機概論20161212
 
Java script functions
Java script   functionsJava script   functions
Java script functions
 
Java script cookies
Java script   cookiesJava script   cookies
Java script cookies
 
Java Script
Java ScriptJava Script
Java Script
 
Tutorial asp.net
Tutorial  asp.netTutorial  asp.net
Tutorial asp.net
 
Function calling in js
Function calling in jsFunction calling in js
Function calling in js
 
course js day 3
course js day 3course js day 3
course js day 3
 
jQuery
jQueryjQuery
jQuery
 
HTML5 New and Improved
HTML5   New and ImprovedHTML5   New and Improved
HTML5 New and Improved
 
Java script frame window
Java script frame windowJava script frame window
Java script frame window
 
Java script frame history
Java script frame historyJava script frame history
Java script frame history
 
Java script page redirection
Java script   page redirectionJava script   page redirection
Java script page redirection
 
JavaServer Pages
JavaServer Pages JavaServer Pages
JavaServer Pages
 

More from Mukund Gandrakota (12)

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

Recently uploaded

(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 

Recently uploaded (20)

(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 

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>