Tải bản đầy đủ - 0 (trang)
Chapter 16. JavaScript: A Web Page Programming Language

Chapter 16. JavaScript: A Web Page Programming Language

Tải bản đầy đủ - 0trang

Chapter16.JavaScript:AWebPage

ProgrammingLanguage

16.1 JAVASCRIPTSYNTAX

16.2 JAVASCRIPTINSIDEOFWEBPAGES

16.3 USERINTERFACESINJAVASCRIPT

16.4 MULTIMEDIAINJAVASCRIPT

16.5 CONCEPTSSUMMARY



ChapterLearningObjectives

Toseevariables,iteration,andconditionalsinanotherprogramminglanguageand

torecognizethemasunderstandable.

TobeabletousealittleJavaScriptinWebpages.

TobeabletohandleeventsinJavaScript.



Whatdootherprogramminglanguageslooklike?Thatquestion

maybeoccurringtoyounow.Youmightalsobechampingat

thebittodosomeprogrammingwiththingsyou'veheard

about,suchasWebpageprogramming.Inthischapter,wedoa

littlebitofeachofthesebyintroducingJavaScript,whichisa

programminglanguagethatcanbeembeddedwithinWeb

pagestoallowcontrolofHTMLandpiecesofWebpages



throughprograms.JavaScriptisn'tthesamelanguageasJava,

buttheydohavemanysimilarities.







[Page529(continued)]



16.1.JavaScriptSyntax

Wecallthelookofaprogramminglanguageitssyntax.Javais

afairlytraditionalprogramminglanguageintermsofitssyntax.

Itdoesn'tlookmuchdifferentthanCorC++orother

languages.JavawasdesignedtomakeiteasyforCorC++

programmerstolearn.

LanguageslikeSchemeandSmalltalklookmuchmoredifferent.

Thereareseveralthingsthatyoushouldexpecttobedifferent

andthatyou'llneedtoexplorewhencomparingthesyntax

(look)ofdifferentlanguages:

Howdoyoudefinevariables?Inmanylanguages,youmust

declarethevariablesandtheirtypes(e.g.,floatingpointvs.

integervs.stringvs.list)beforeyoucanassignthem.

Howiseachlineconstructed?InlanguageslikeJavaandC,

eachlinemustendwithasemicolon.



[Page530]

Howareblocksdefined?Howdoyougroupstatementsfor

loops,conditionals,andfunctionbodies?

Howdoyoudefinefunctions,procedures,methods,classes,

andsoon?

JavaScriptismeanttobeascriptinglanguage.Thismeansthat

it'smeanttobeusedeasily,bynon-professionalprogrammers,

tosolverelativelysimpletasks.Nooneisgoingtouse

JavaScripttowritethemastercalculationprogramfortheIRS,



norsomethingthattracksalltheaccountsforownersof

MasterCards.It'sdesignedtolookalotlikeJava(andC),to

makeiteasiertopickupforpeoplefamiliarwiththosekindsof

languages.

JavaScriptcanbeusedtoprogramtheWebserverorinteract

withthevieweroftheWebpageontheclient.We'regoingto

emphasizethelatter.WhenJavaScriptisusedonaclient

machine,it'sactuallyexecutedbythebrowseryourbrowser

containsaJavaScriptinterpreter.Wecallthisclient-side

JavaScript.

InJavaScript,variablesaresupposedtobedeclaredbeforeuse,

butyoudon'thavetospecifytheirtype.Thetypewillbe

determinedbasedonhowyouuseit.However,JavaScriptisn't

strictaboutthisyoucansimplysaya=12;.Butit'smore

correcttodeclarethevariableassomethingyou'llbeusingby

eithertypingsomethinglikevara=12;or:

vara;

...

a=12;



LikeJava,JavaScriptdefinesblocksusingcurlybraces.Youcan

useanyindentationyouwant,buttheblockbeginswithacurly

braceandendswithaclosecurlybrace.

functiontest()

{

document.writeln("Thisisatest");

}



InJavascriptyoucandefinefunctionswhicharelikeJava

methods.Theabovecodedefinesafunctionnamedtestthat



doesn'ttakeanyparametersandwritestotheHTMLdocument

thestringThisisatest.

We'llfindmanysimilaritiesanddifferencesbetweenJavaand

JavaScriptwhenwegetdowntowritingindividuallines.

LikeJava,JavaScriptendseachlinewithasemicolon(;).

Youcanbreakuplinessothattheyarereadabletoyou,as

longaseachendswithasemicolon.

varmyvariable=(4*x)+

(5*y);

JavaScript'sforisthesameasJava'sforloop.Itisused

whenyouknowhowmanytimesaloopwillbeexecuted.

Afterthekeywordforcomesaparenthesizedlistofthree

expressionsseparatedbysemicolons.Thefirstexpressionis

evaluatedbeforetheforloopbegins.Thesecondexpression

isevaluatedattheendoftheforlooptoseeifwecontinue

loopingiftheexpressionistrue,wekeeplooping.Thethird

expressioniswhattodobeforerepeatingtheloop.We'llsee

aforlooplaterinthischapter.



[Page531]

InJavaScript,weusewriteandwritelninsteadofJava's

System.out.printandSystem.out.println,andinsteadofwriting

toaCommandArea,we'llactuallybewritingtotheHTML

pageitself!

Butinarealsense,thesearejustdetails.Thebasicoperation

ofJavaScriptisnotunlikeJava.Statementsarestillexecuted

oneaftertheother,therearestillvariables,loops,and

conditionals,andwe'llstillhavefunctionsandmethods.Allof

whatyouknewbeforestillapplies.



JavaScriptisallaboutobjects.Justabouteveryfunctionis

actuallyamethod.Thereisn'taglobalprint,andthere'snot

evenaglobalwriteorwriteln.Instead,towriteintothe

document,we'llusedocument.write()(or,toendthelinewitha

newline,document.writeln()).ThedocumenthereistheHTML

documentitself.







[Page531(continued)]



16.2.JavaScriptInsideofWebPages

JavaScriptsitsinsideofHTMLpagesthereisnoProgramArea,

norseparateJavaScriptfiles.[1]YouusethetagstoembedtheJavaScriptintotheHTML.