Вы находитесь на странице: 1из 6

What is JavaScript? JavaScript is scripting language used for client side scripting.

JavaScript developed by Netscape in 1995 as a method for validating forms and providing interactive content to web site. lthough it shares many of the features and structures of the full !"#$%&'N( )http*++www.webopedia.com+,$%-+J+Java.html) .Java language/0 it was developed independently. Javascript can interact with !"#$%&'N( )http*++www.webopedia.com+,$%-+!+!,-&.html) .!,-&/ !"#$%&'N( )http*++www.webopedia.com+,$%-+S+source1code.html) .source code/0 enabling Web authors to spice up their sites with !"#$%&'N( )http*++www.webopedia.com+,$%-+2+dynamic.html) .dynamic/ content. -icrosoft and Netscape introduced JavaScript support in their browsers. 3enefits of JavaScript 4ollowing are the benefits of JavaScript. associative arrays loosely typed variables regular e5pressions ob6ects and classes highly evolved date0 math0 and string libraries W78 29- support in the JavaScript 2isadvantages of JavaScript 2eveloper depends on the browser support for the JavaScript ,here is no way to hide the JavaScript code in case of commercial application $5ample :html; :body; :h1;-y 4irst Web #age:+h1; :script type<)te5t+6avascript); document.write=)2ate* >? 2ate=@ @A :+script; :+body; :+html; ,he lines between the :script; and :+script; contain the JavaScript and are e5ecuted by the browser. JavaScript Statements JavaScript is a seBuence of statements to be e5ecuted by the browser. JavaScript is 8ase Sensitive and terminated with semicolon JavaScript 3locCs JavaScript statements can be grouped together in blocCs. 3locCs start with a left curly bracCet D0 and end with a right curly bracCet E. ,he purpose of a blocC is to maCe the seBuence of statements e5ecute together. ,his e5ample will write a heading and two paragraphs to a web page* :script type<)te5t+6avascript); D document.write=):h1;,his is a heading:+h1;)@A document.write=):p;,his is a paragraph.:+p;)@A document.write=):p;,his is another paragraph.:+p;)@A E :+script; JavaScript Fariables JavaScript variables are used to hold values or e5pressions. a more descriptive variable can have a short name0 liCe 50 or

%ules for JavaScript variable names* Fariable names are case sensitive =y and " are two different variables@ Fariable names must begin with a letter or the underscore character "ou declare JavaScript variables with the var Ceyword* var 5<1GGA var name<)%atHA &ocal JavaScript Fariables Ilobal JavaScript Fariables 9perators JavaScript rithmetic 9perators 9perator2escription$5ample%esult? ddition5<y?J5<Ky<5LSubtraction5<yL J5<7y<5M-ultiplication5<yMJ5<1Gy<5+2ivision5<y+J5<J.5y<5N-odulus =division remainder@5<y NJ5<1y<5??'ncrement5<??y5<Oy<O5<y??5<5y<OLL2ecrement5<LLy5<Py<P5<yLL5<5y<P JavaScript ssignment 9perators 9perator$5ampleSame s%esult<5<y 5<5?<5?<y5<5?y5<15L<5L<y5<5L y5<5M<5M<y5<5My5<5G+<5+<y5<5+y5<JN<5N<y5<5Ny5<G ,he ? 9perator Qsed on Strings ,he ? operator can also be used to add string variables or te5t values together. t5t1<)What a very)A t5tJ<)nice day)A t5t7<t5t1?t5tJA t5t1<)What a very )A t5tJ<)nice day)A t5t7<t5t1?t5tJA fter the e5ecution of the statements above0 the variable t5t7 contains* )What a very nice day) 8omparison 9perators 8omparison operators are used in logical statements to determine eBuality or difference between variables or values. Iiven that 5<50 the table below e5plains the comparison operators* 9perator2escription$5ample<<is eBual to 5<<R is false 5<<5 is true <<<is e5actly eBual to =value and type@5<<<5 is true 5<<<)5) is falseS<is not eBual5S<R is true;is greater than5;R is false:is less than5:R is true;<is greater than or eBual to5;<R is false:<is less than or eBual to5:<R is true&ogical 9perators &ogical operators are used to determine the logic between variables or values. Iiven that 5<O and y<70 the table below e5plains the logical operators* 9perator2escription$5ampleTTand=5 : 1G TT y ; 1@ is trueUUor=5<<5 UU y<<5@ is falseSnotS=5<<y@ is true

Java Script rray 8oncepts. n array is a special variable0 which can hold more than one value0 at a time. 'f you have a list of items =a list of car names0 for e5ample@0 storing the cars in single variables could

looC liCe this* var car1<)Saab)A var carJ<)Folvo)A var car7<)3-W)A 8reate an rray var my8ars<new rray=@A ++ regular array =add an optional integer my8arsVGW<)Saab)A ++ argument to control arrayXs siYe@ my8arsV1W<)Folvo)A my8arsVJW<)3-W)A :script type<)te5t+6avascript); :SLL var my rray < new rray=@A my rrayVGW < )4ootball)A my rrayV1W < )3aseball)A my rrayVJW < )8ricCet)A document.write=my rrayVGW ? my rrayV1W ? my rrayVJW@A ++LL; :+script; JavaScript rray 9b6ect 2escribes the JavaScript array ob6ect including parameters0 properties0 and methods. #arameters array&ength elementN L rray element list of values #roperties inde5 input length L ,he Buantity of elements in the ob6ect. prototype L 4or creating more properties. -ethods chop=@ L Qsed to truncate the last character of a all strings that are part of an array. ,his method is not defined so it must be written and included in your code. var e5clamations < new rray=)&ooC outS)0 )2ucCS) @ e5clamations.chop=@ 8auses the values of e5clamations to become* &ooC out 2ucC concat=@ grep=search string@ L ,aCes an array and returns those array element strings that contain matching strings. ,his method is not defined so it must be written and included in your code. 6oin=delimiter@ L #uts all elements in the array into a string0 separating each element with the specified delimiter. pop=@ L #ops the last string off the array and returns it. ,his method is not defined so it must be written and included in your code. push=strings@ L Strings are placed at the end of the array. ,his method is not defined so it must be written and included in your code.

reverse=@ L #uts array elements in reverse order. shift=@ L 2ecreases array element siYe by one by shifting the first element off the array and returning it. ,his method is not defined so it must be written and included in your code. sort=@ L Sorts the array elements in dictionary order or using a compare function passed to the method. splice=@ L 't is used to taCe elements out of an array and replace them with those specified. 'n the below e5ample the element starting at element 7 is removed0 two of them are removed and replaced with the specified strings. ,he value returned are those values that are replaced. ,his method is not defined so it must be written and included in your code. split=deliimiter@ L Splits a string using the delimiter and returns an array. unshift=@ L #laces elementa at the start of an array $5amples 1. rray concatenation :html; :body; :script type<)te5t+6avascript); var parents < V)Jani)0 ),ove)WA var children < V)8ecilie)0 )&one)WA var family < parents.concat=children@A document.write=family@A :+script; :+body; :+html; JavaScript 9b6ect !ierarchy ,here are two main types of JavaScript ob6ects. &anguage 9b6ects L 9b6ects provided by the language and are not dependent on other ob6ects. Navigator L 9b6ects provided by the client browser. ,hese ob6ects are all sub ob6ects to the navigator ob6ect. ,he JavaScript !ierarchy 'N8&Q2$#'8,Q%$ )http*++www.comptechdoc.org+independent+web+cgi+6avamanual+ob6heirarchy.gif) ZM -$%I$49%- ,'N$, . / 3uiltL'n 4unctions Simple -essage 3o5 alert=What,o2isplay@A ,he -essage 3o5 With 9ptions message bo5 created with the alert=@ function is used only to display a message to the user and it is eBuipped with the 9( button. 'n some cases you may want to asC a specific Buestion to the user and e5pect the user to maCe a decision. ,o do this0 the !"#$%&'N( )http*++www.function5.com+6avascript+&essonGO.htm) Zl )[) .JavaScript/ language allows you to create a message eBuipped with two buttons0 namely 9( and 8ancel. confirm=@ function. 'ts synta5 is* confirm=\uestion,o sC@A $e5ample*7 :Script &anguage<)JavaScript); function application%eminder=@ D confirm=),he application you filled out is not complete.Zn2o you still wanto to Buit?)@A E :+Script;

:input type<)button) value<)%eminder) on8licC<)application%eminder=@); 8onversion 4unctions 'nteger 8onversion ,he parset'nt=@ function is used to analyYe what is in a te5tLbased control. 'ts synta5 is* parse'nt=Falue@ parse'nt=)1JP)@ 2ecimal 8onversion parse4loat=@ function. 'ts synta5 is* parse4loat=Falue@ #recision 8onversion to#recision=@ function. 'ts synta5* Falue.to#recision=number@ onversion to 4i5ed 2ecimal to4i5ed=@ function. 'ts synta5 is* Falue.to4i5ed=number9f2igits@ $vents ,he building blocCs of an interactive web page is the JavaScript event system. n event in JavaScript is something that happens with or on the webpage. few e5ample of events* mouse clicC ,he webpage loading -ousing over a hot spot on the webpage0 also Cnown as hovering Selecting an input bo5 in an !,-& form CeystroCe Simple $5ample :html; :head; :script type<)te5t+6avascript); :SLL function popup=@ D alert=)!ello World)@ E ++LL; :+script; :+head; :body; :input type<)button) value<)8licC -eS) onclicC<)popup=@);:br +; :a href<)[) onmouseover<)) on-ouseout<)popup=@); !over -eS:+a; :+body; :+html; on3lur $vent !andler calls the valid=@ function to confirm that XdataX has a legal value. Note that the Ceyword this is used to refer to the current ob6ect. on8hange on8hange $vent !andler calls the valid=@ function which alerts the user about value that has been

inputted. 'n an on8licC $vent !andler0 JavaScript function is called when an ob6ect in a button =regular0 radio0 reset and submit@ is clicCed0 a linC is pushed0 a checCbo5 is checCed or an image map area is selected. $5cept for the regular button and the area0 the on8licC $vent !andler can return false to cancel the action. n on4ocus $vent !andler e5ecutes JavaScript code when input focus enters the field either by tabbing in or by clicCing but not selecting input from the field. on&oad* n on&oad event occurs when a window or image finishes loading. on-ouse9ut* JavaScript code is called when the mouse leaves a specific linC or an ob6ect or area from outside that ob6ect or area. on-ouse9ver* JavaScript code is called when the mouse is placed over a specific linC or an ob6ect or area from outside that ob6ect or area. on%eset* on%eset $vent !andler e5ecutes JavaScript code when the user resets a form by clicCing on the reset button. onSelect* onSelect $vent !andler e5ecutes JavaScript code when the user selects some of the te5t within a te5t or te5tarea field. onSubmit* n onSubmit $vent !andler calls JavaScript code when the form is submitted. onQnload* n onQnload $vent !andler calls JavaScript code when a document is e5ited. V !"#$%&'N( )http*++www.techotopia.com+inde5.php? title<JavaScript14low18ontrol1and1&oopingTaction<editTsection<1G) Zo )$dit section* label Statements) .edit/W label Statements 9ne problem that can arise using the breaC statement involves breaCing from a loop that is nested inside another loop. 'n the following e5ample the breaC will breaC out of the inner loop0 but not the outer loop* for =i < GA i : 1GGGA i??@ D for =5 < GA 5 : 1GGA 5??@ D if =5 << 1G@ D breaCA E E @

Вам также может понравиться