JavaScript Is A Scripting Language
JavaScript Is A Scripting Language
1. A scripting language is a lightweight programming language. 2. JavaScript is programming code that can be inserted into HTML pages. 3. JavaScript code can be e ecuted b! all modern web browsers. ". JavaScript is eas! to learn.
Write to the Document with JavaScript <!DOCTYPE html> <html> <body> <h1>My First Web Page</h1> <scri t> doc!me"t#$rite%&< >My First 'a(a)cri t</ >&*+ </scri t> </body> </html> JavaScript statements. <!DOCTYPE html> <html> <body> <h1>My Web Page</h1> < id,&demo&>- Paragra h#</ > <di( id,&myD./&>- D./#</di(> <scri t> doc!me"t#getEleme"t0y.d%&demo&*#i""er1TM2,&1ello Dolly&+ doc!me"t#getEleme"t0y.d%&myD./&*#i""er1TM2,&1o$ are yo!3&+ </scri t> </body> </html> JavaScript blocks. <!DOCTYPE html> <html> <body> <h1>My Web Page</h1> < id,&myPar&>. am a aragra h#</ > <di( id,&myDi(&>. am a di(#</di(> < > <b!tto" ty e,&b!tto"& o"clic4,&myF!"ctio"%*&>Try it</b!tto"> </ > <scri t> 5!"ctio" myF!"ctio"%* 6
doc!me"t#getEleme"t0y.d%&myPar&*#i""er1TM2,&1ello Dolly&+ doc!me"t#getEleme"t0y.d%&myDi(&*#i""er1TM2,&1o$ are yo!3&+ 7 </scri t> < >Whe" yo! clic4 o" &Try it&8 the t$o eleme"ts $ill cha"ge#</ > </body> </html> Declare a variable, assign a value to it, and display it <!DOCTYPE html> <html> <body> <scri t> (ar 5irst"ame+ 5irst"ame,&1ege&+ doc!me"t#$rite%5irst"ame*+ doc!me"t#$rite%&<br>&*+ 5irst"ame,&To(e&+ doc!me"t#$rite%5irst"ame*+ </scri t> < >The scri t abo(e declares a (ariable8 assig"s a (al!e to it8 dis lays the (al!e8 cha"ges the (al!e8 a"d dis lays the (al!e agai"#</ > </body> </html> If...else statement <!DOCTYPE html> <html> <body> < >Clic4 the b!tto" to get a time9based greeti"g#</ > <b!tto" o"clic4,&myF!"ctio"%*&>Try it</b!tto"> < id,&demo&></ > <scri t> 5!"ctio" myF!"ctio"%* 6 (ar :,&&+ (ar time,"e$ Date%*#get1o!rs%*+ i5 %time<;<* 6 :,&=ood day&+ 7 else 6 :,&=ood e(e"i"g&+ 7 doc!me"t#getEleme"t0y.d%&demo&*#i""er1TM2,:+ 7
</scri t> </body> </html> Switch statement <!DOCTYPE html> <html> <body> < >Clic4 the b!tto" to dis lay $hat day it is today#</ > <b!tto" o"clic4,&myF!"ctio"%*&>Try it</b!tto"> < id,&demo&></ > <scri t> 5!"ctio" myF!"ctio"%* 6 (ar :+ (ar d,"e$ Date%*#getDay%*+ s$itch %d* 6 case <> :,&Today is )!"day&+ brea4+ case 1> :,&Today is Mo"day&+ brea4+ case ;> :,&Today is T!esday&+ brea4+ case ?> :,&Today is Wed"esday&+ brea4+ case @> :,&Today is Th!rsday&+ brea4+ case A> :,&Today is Friday&+ brea4+ case B> :,&Today is )at!rday&+ brea4+ 7 doc!me"t#getEleme"t0y.d%&demo&*#i""er1TM2,:+ 7 </scri t> </body> </html> lert bo! with line breaks <!DOCTYPE html>
<html> <body> < >Clic4 the b!tto" to demo"strate li"e9brea4s i" a o ! bo:#</ > <b!tto" o"clic4,&myF!"ctio"%*&>Try it</b!tto"> < id,&demo&></ > <scri t> 5!"ctio" myF!"ctio"%* 6 alert%&1elloC"1o$ are yo!3&*+ 7 </scri t> </body> </html> "onfirm bo! <!DOCTYPE html> <html> <body> < >Clic4 the b!tto" to dis lay a co"5irm bo:#</ > <b!tto" o"clic4,&myF!"ctio"%*&>Try it</b!tto"> < id,&demo&></ > <scri t> 5!"ctio" myF!"ctio"%* 6 (ar :+ (ar r,co"5irm%&Press a b!tto"!&*+ i5 %r,,tr!e* 6 :,&Yo! ressed OD!&+ 7 else 6 :,&Yo! ressed Ca"cel!&+ 7 doc!me"t#getEleme"t0y.d%&demo&*#i""er1TM2,:+ 7 </scri t> </body> </html> #unction with arguments, that returns a value <!DOCTYPE html> <html> <body> < >This e:am le calls a 5!"ctio" $hich er5orms a calc!latio"8 a"d ret!r"s the res!lt></ > < id,&demo&></ > <scri t>
5!"ctio" myF!"ctio"%a8b* 6 ret!r" aEb+ 7 doc!me"t#getEleme"t0y.d%&demo&*#i""er1TM2,myF!"ctio"%@8?*+ </scri t> </body> </html> #or loop <!DOCTYPE html> <html> <body> < >Clic4 the b!tto" to loo thro!gh a bloc4 o5 code 5i(e times#</ > <b!tto" o"clic4,&myF!"ctio"%*&>Try it</b!tto"> < id,&demo&></ > <scri t> 5!"ctio" myF!"ctio"%* 6 (ar :,&&8i+ 5or %i,<+i<A+iFF* 6 :,: F &The "!mber is & F i F &<br>&+ 7 doc!me"t#getEleme"t0y.d%&demo&*#i""er1TM2,:+ 7 </scri t> </body> </html> While loop <!DOCTYPE html> <html> <body> < >Clic4 the b!tto" to loo thro!gh a bloc4 o5 as lo"g as <em>i</em> is less tha" A#</ > <b!tto" o"clic4,&myF!"ctio"%*&>Try it</b!tto"> < id,&demo&></ > <scri t> 5!"ctio" myF!"ctio"%* 6 (ar :,&&8i,<+ $hile %i<A* 6 :,: F &The "!mber is & F i F &<br>&+ iFF+ 7 doc!me"t#getEleme"t0y.d%&demo&*#i""er1TM2,:+
7 </scri t> </body> </html> Do While loop <!DOCTYPE html> <html> <body> < >Clic4 the b!tto" to loo thro!gh a bloc4 o5 as lo"g as <em>i</em> is less tha" A#</ > <b!tto" o"clic4,&myF!"ctio"%*&>Try it</b!tto"> < id,&demo&></ > <scri t> 5!"ctio" myF!"ctio"%* 6 (ar :,&&8i,<+ do 6 :,: F &The "!mber is & F i F &<br>&+ iFF+ 7 $hile %i<A* doc!me"t#getEleme"t0y.d%&demo&*#i""er1TM2,:+ 7 </scri t> </body> </html> $reak and continue a loop < >Clic4 the b!tto" to do a loo $hich $ill s4i the ste $here i,?#</ > <b!tto" o"clic4,&myF!"ctio"%*&>Try it</b!tto"> < id,&demo&></ > <scri t> 5!"ctio" myF!"ctio"%* 6 (ar :,&&8i,<+ 5or %i,<+i<1<+iFF* 6 i5 %i,,?* 6 co"ti"!e+ 7 :,: F &The "!mber is & F i F &<br>&+ 7 doc!me"t#getEleme"t0y.d%&demo&*#i""er1TM2,:+ 7 </scri t> </body>
</html> cting to the onclick event <!DOCTYPE html> <html> <head> <scri t> 5!"ctio" dis layDate%* 6 doc!me"t#getEleme"t0y.d%&demo&*#i""er1TM2,Date%*+ 7 </scri t> </head> <body> <h1>My First 'a(a)cri t</h1> < id,&demo&>This is a aragra h#</ > <b!tto" ty e,&b!tto"& o"clic4,&dis layDate%*&>Dis lay Date</b!tto"> </body> </html> cting to the onmouseover event <!DOCTYPE html> <html> <head> <scri t> 5!"ctio" $riteTe:t%t:t* 6 doc!me"t#getEleme"t0y.d%&desc&*#i""er1TM2,t:t+ 7 </scri t> </head> <body> <img src ,& la"ets#gi5& $idth ,&1@A& height ,&1;B& alt,&Pla"ets& !sema ,&G la"etma & /> <ma "ame,& la"etma &> <area sha e ,&rect& coords ,&<8<8H;81;B& o"mo!seo(er,&$riteTe:t%IThe )!" a"d the gas gia"t la"ets li4e '! iter are by 5ar the largest obJects i" o!r )olar )ystem#I*& hre5 ,&s!"#htm& target ,&Kbla"4& alt,&)!"& /> <area sha e ,&circle& coords ,&L<8AH8?& o"mo!seo(er,&$riteTe:t%IThe la"et Merc!ry is (ery di55ic!lt to st!dy 5rom the Earth beca!se it is al$ays so close to the )!"#I*& hre5 ,&merc!r#htm& target ,&Kbla"4& alt,&Merc!ry& /> <area sha e ,&circle& coords ,&1;@8AH8H& o"mo!seo(er,&$riteTe:t%IM"til the 1LB<s8 /e"!s $as o5te" co"sidered a t$i" sister to the Earth beca!se /e"!s is the "earest la"et to !s8 a"d beca!se the t$o la"ets seem to share ma"y characteristics#I*&
hre5 ,&(e"!s#htm& target ,&Kbla"4& alt,&/e"!s& /> </ma > < id,&desc&>Mo!se o(er the s!" a"d the la"ets a"d see the di55ere"t descri tio"s#</ > </body> </html> Simple timing <!DOCTYPE html> <html> <body> < >Clic4 the b!tto" to $ait ? seco"ds8 the" alert &1ello&#</ > <b!tto" o"clic4,&myF!"ctio"%*&>Try it</b!tto"> <scri t> 5!"ctio" myF!"ctio"%* 6 setTimeo!t%5!"ctio"%*6alert%&1ello&*78?<<<*+ 7 </scri t> </body> </html> clock created with a timing event <!DOCTYPE html> <html> <head> <scri t> 5!"ctio" startTime%* 6 (ar today,"e$ Date%*+ (ar h,today#get1o!rs%*+ (ar m,today#getMi"!tes%*+ (ar s,today#get)eco"ds%*+ // add a Nero i" 5ro"t o5 "!mbers<1< m,chec4Time%m*+ s,chec4Time%s*+ doc!me"t#getEleme"t0y.d%It:tI*#i""er1TM2,hF&>&FmF&>&Fs+ t,setTimeo!t%5!"ctio"%*6startTime%*78A<<*+ 7 5!"ctio" chec4Time%i* 6 i5 %i<1<* 6 i,&<& F i+ 7 ret!r" i+ 7 </scri t>