Thi -bk rght 2017 Stephen Blumenthal with ll right rrvd. It i illgl t , ditribut, r rt drvtv wrk frm th bk n whl r n rt. N rt f th rrt m b rrdud r trnmttd n n frm whtvr, ltrni, r mhnil, nludng htng, rrdng, r b n infrmtinl trg r rtrivl system wthut xrd writtn, dtd nd ignd rmn frm th uthr.
Introduction t JavaScript
JvSrit i n intrrtd rgrmming lngug, built n th ECMASrit tndrd. Th language dfinitin i rll brd in it can b dfind rdurl lngug bd n rtt, imrtiv, wkl td, nd dnmi.
JvSrit i mainly ud a lint id rgrmming lngug imlmntd rt f a wb brwr t llw dvlr n imrvd w t imlmnt ur intrf nd dnmi ftur in wb g, lthugh thr r imlmnttin f JvSrit n th rvr id (SSJS) th ulrit f th lngug i du t th lint id imlmnttin ln. JvSrit n l b fund utid wb litin, fr xml a w t dd intrtivit t PDF dumnt nd dkt widgt.
JvSrit w dignd with a imilr ntx C, lthugh it tk nm nd nvntin frm th Jv rgrmming lngug. Hwvr, dit th nm Jv nd JvSrit r nt rltd nd hv diffrnt mnti nd ur.
JavaScript w originally dvld b Brndn Eih f Nt undr th nm Mh, whih w ltr rnmd t LivSrit, t finll bing lld JvSrit. The nm hng inidd rximtl with th mmnt in whih Nt ddd support fr Jv thnlg in it wb brwr Nt Navigator vrin 2.0B3 in lt 1995. Th name JvSrit w nfuin, giving th imrin tht th lngug i n xtnin f Jv, nd it h bn hrtrizd b mn a mrkting trtg fr Nt t gin rtig and innvt in wht wr th nw wb rgrmming lngug.
Th fllwing r Mirft imlmntd a imilr lint id rgrmming lngug rt f it Intrnt Exlrr 3.0 wb brwr. Mirft lld it lint id lngug "jrit", t vid problems rltd t th brnd. Th Jrit trm m similar tht th bth "javascript" and "jrit" r ftn ud intrhngbl, but th ifitin f JSrit i nt 100% mtibl with the ECMA ifitin.
T vid th inmtibiliti, th Wrld Wid Wb Consortium (W3C) dignd th tndrd Dumnt Objt Mdl (DOM, or dumnt bjt mdl), whih w inrrtd in th vrin 6 f Intrnt Exlrr nd Nt Nvigtr, Or vrin 7, Mzill Firfx in it firt rl, nd ll mdrn brwr thrftr.
In 1997 thr was a rl t ubmit JvSrit t th tndrd f th Eurn Cmutr Mnufturr ' Aitin ECMA, whih dit it nm i nt Eurn but intrntinl, bd in Gnv. In Jun 1997, it w dtd n ECMA tndrd undr th nm f ECMASrit. JavaScript also bm n ISO tndrd.
Bu f it tndrdiztin nd th grt dtin f the intrnt, JvSrit h bm th mt ud rgrmming lngug in th lnt.
Nt: JvSrit i a rgitrd trdmrk f Orl Crrtin. It i ud undr lin b th rdut rtd b Nt Cmmunitin nd urrnt ntiti uh th Mzill Fundtin.
U f JavaScript
JavaScript i present in most wb g td. Chances r tht th page u r looking at right nw ntin th d for JvSrit. Tr thi tivit: Right-click n a web g, then lik 'Viw Sur'. Yu huld be able to find the word JavaScript somewhere in th code of th g.
Whil HTML markup language llw web developers t format content, JavaScript llw them t mk th g dnmi. Fr xml, HTML allows fr mking txt bold, rting text boxes, nd rting buttn, whr JvSrit allows for changing txt n the page, rting -u messages, and validating txt in text bx t make ur r uird fild have been filld. JavaScript mk wb pages more dnmi b llwing users to intrt with web g, click on lmnt, nd change the g.
What JvSrit n d for u
Lt tk a t bk and unt th merits f JavaScript:
- JvSrit is very easy to imlmnt. All u nd to d i put your d in th HTML dumnt and tll th brwr that it is JavaScript.
- JvSrit wrk n wb users mutr - vn when they r fflin!
- JvSrit llw you t rt highl rniv intrf tht improve th ur experience and rvid dnmi functionality, withut hving t wit fr the rvr t rt and hw another page.
- JavaScript can load content into th dumnt if and whn the ur nd it, withut reloading th ntir g this i mmnl rfrrd t as Ajx.
- JvSrit n tt for wht i ibl in your brwr nd rt rdingl this i called Prinil f unbtruiv JvSrit r mtim dfniv Sriting.
- JavaScript n help fix brwr rblm or patch hl in brwr urt for example fixing CSS lut issues in certain brwr.
Tht i a lot fr a lngug tht until recently was lughd at b rgrmmr favouring highr rgrmming languages. On rt f the rnin f JavaScript i tht w r building mr and more mlx wb litin these days, and high interactivity ithr requires Flash (or thr lugin) r riting. JavaScript is arguably th best way to g, it i a web tndrd, it i supported ntivl r brwr (mr r less some thing diffr r brwr, nd th diffrn are discussed in appropriate places in th articles tht follow thi n), and it i mtibl with thr n web tndrd.
Cmmn u of JvSrit
Th ug f JavaScript h hngd over th years wv bn uing it. At firt, JavaScript intrtin with th site was mostly limitd t interacting with frm, giving fdbk t th ur and dtting whn th d rtin things. W ud lrt() t ntif th user f mthing ( Figur 1), nfirm() t k if mthing i OK t d or not and ithr rmt() r a frm fild to get user inut.
Figur 1: Telling th nd user but rrr using n alert() statement w all w uld d in th rl d f JvSrit. Nithr rtt nr ubtl.
This ld mtl to vlidtin rit tht td the ur t send a frm to th server when there was a mitk, and iml converters nd lultr. In dditin, w l managed to build highl ul thing lik rmt asking th user fr thir name jut to print it ut immditl ftrwrd.
Anthr thing w ud was document.write() t add content to th dumnt. We l wrkd with u windw and frm nd lt mn a nerve and ulld ut hir tring t mk them tlk t h other. Thinking about mt f th technologies huld mk n dvlr rk frwrd nd bkwrd and curl u into a fetal itin tmmring mk thm go w, lt' nt dwll on these thing thr are better w t u JavaScript!
Entr DOM riting
Whn brwr started urting and imlmnting th Dumnt Objt Mdl (DOM), whih llw us t hv much rihr interaction with web pages, JvSrit trtd to gt more intrting.
Th DOM is n bjt representation f the document. Th previous rgrh fr example (hk out it source uing viw ur) in DOM k i an lmnt nd with a nodeName f p. It contains thr hild nd a txt nd ntining Whn brwr trtd urting nd implementing th it nodeValue, n element nd with a ndNm f , nd nthr text nd with a ndVlu f , which llw u t hv much rihr interaction with web g, JvSrit trtd t gt mr interesting.. Th a child nd l h an ttribut node lld hrf with a value nd a hild nd that is a text node with a nodeValue of Document Objt Mdl(DOM).
Yu could also rrnt this rgrh viull uing a tr diagram, as n in Figur 2.
Figur 2: A viul rrnttin f ur ml DOM tr.
In human wrd u can that the DOM xlin bth th t, the vlu and the hierarchy f everything in the document u dnt nd to knw nthing mr fr now.
- A any lmnt in th dumnt nd manipulate its lk, content nd ttribut.
- Crt nw lmnt nd content and apply them to th document when nd if they r needed.
Thi means tht w dnt have to rely n windows, frm, frm nd ugl lrt n lngr, nd n give feedback t the ur in th dumnt in a nil tld mnnr, inditd in Figure 3.
Figure 3: Uing the DOM u n rt nicer nd l intruiv rrr messages.
Tgthr with event handling thi i a vr powerful arsenal to rt interactive nd beautiful intrf.
Evnt hndling means that our d reacts t thing tht hn in the brwr. This uld b thing that hn utmtill lik th g finihing loading but most f the tim w rt t what th ur did to the brwr.
Ur might riz th windw, rll th g, r rtin k r click on link, buttn and lmnt using th mu. With vnt handling we can wit fr th thing to happen nd tell th wb page t respond to th actions as w wih. Whr in th past, liking any link wuld tk th it viitr t another dumnt, w can nw hijack thi funtinlit nd d something l lik showing nd hiding a panel or tking the infrmtin in th link nd uing it t nnt t a web rvi.