You are on page 1of 1

lrlroduc|rg S(docurerl).

ready()
TIIs Is LIe IIrsL LIIng Lo Ieurn ubouL jQuery: I you wunL un evenL Lo work on your puge, you sIouId
cuII IL InsIde LIe $(document).ready() IuncLIon. EveryLIIng InsIde IL wIII Ioud us soon us LIeDOM Is
Iouded und beIore LIe puge conLenLs ure Iouded.
PLAN TEXT
JavaScript:
1. $(document).ready(function() ,
2. // put all your jQuery goodness in here.
. ,);


TIe $(document).ready() IuncLIon Ius u Lon oI udvunLuges over oLIer wuys oI geLLIng evenLs Lo work.
IrsL oI uII, you don'L Iuve Lo puL uny "beIuvIoruI" murkup In LIe HTM. You cun sepuruLe uII oI your
juvuscrIpLJjQuery InLo u sepuruLe IIIe wIere IL's eusIer Lo muInLuIn und wIere IL cun sLuy ouL oI LIe
wuy oI LIe conLenL. never dId IIke seeIng uII LIose "juvuscrIpL:voId()" messuges In LIe sLuLus bur
wIen wouId Iover over u IInk. TIuL's wIuL Iuppens wIen you uLLucI LIe evenL dIrecLIy InsIde un <a
href> Lug.
On some puges LIuL use LrudILIonuI juvuscrIpL, you'II see un "onIoud" uLLrIbuLe In LIe <body> Lug. TIe
probIem wILI LIIs Is LIuL IL's IImILed Lo onIy one IuncLIon. OI yeuI, und IL udds "beIuvIoruI" murkup
Lo LIe conLenL uguIn. Jeremy KeILI's exceIIenL book, DOM ScrIpLIng, sIowed me Iow Lo creuLe un
uddoudEvenL IuncLIon Lo u sepuruLe juvuscrIpL IIIe LIuL uIIows Ior muILIpIe IuncLIons Lo be Iouded
InsIde IL. BuL IL requIres u IuIr umounL oI code Ior someLIIng LIuL sIouId be ruLIer sLruIgILIorwurd.
AIso, IL LrIggers LIose evenLs wIen LIe 3/4 Iouds, wIIcI Ieuds me Lo unoLIer udvunLuge oI
$(documenL).reudy().
WILI $(document).ready(), you cun geL your evenLs Lo Ioud or IIre or wIuLever you wunL LIem Lo
do -01470 LIe wIndow Iouds. EveryLIIng LIuL you sLIck InsIde ILs bruckeLs Is reudy Lo go uL LIe eurIIesL
possIbIe momenL - us soon us LIe DOM Is regIsLered by LIe browser, wIIcI uIIows Ior some nIce
IIdIng und sIowIng eIIecLs und oLIer sLuII ImmedIuLeIy wIen LIe user IIrsL sees LIe puge eIemenLs.

You might also like