ButwhenitcomestoJavascript, a normalscripttagwillsend a requesttodownloadandtheParsonsstopsassoonasithitsthatscript, TechandWaitsuntilthescripttagisdownmoodandexecutedbeforeit'llcontinue.
PartsintherestofHTMLfiles, andthisisthefirstwaytoolowJavascript, whichisthewaythatmostJavaScriptisloadedbecauseit's just a singlescripttagwith a source, attributesandnootherattributes.
Andthismeansthatifyouhave a bunchofasynchronousJavaScripttagsinyourproject, theywillbeloadedin a randomorder, dependingonhowfasttheactualfiledownloads.
Soeverytimeyouloadthepage, theycouldpotentiallyberunin a differentorder.
Mostparts, inmyopinion, differ, isthemostusefulofthethreewaystoloadtheJavascriptbecauseitallowsyoutorunJavascriptinorder, whichissometimesveryimportantifyou'reincludingcertainlibrariesthatdependonotherlibraries, soyouneedtoloadthemin a veryspecificorder.
Or, ifyoudon't want a parse, blocktheparsonatall.
Soyoudon't wanttouse a sing, sinceifitfinishesdownloading, itwillblocktheparcelwhileit's executingthedrivers, which, dependingonthesizeofthefileandthecontentinit, couldbeaninsect, a noninsignificantamountoftimethatyou'rewastingrunningtheJavascriptwhenyoucouldbedoingthataftertheHTmostparsedandit'llruninorder, whichisverynicetoehavenowthatweunderstandhowthedifferand a sinkattributesworkandhowdramascriptcaneitherblockornotblocktheparsingofourHTMountain.
Let's take a lookat a liveexampleofusingthesedifferentattributestoelowJavaScriptsowecanbetterunderstandhowthesedifferentattributesaffecttheloadspeedofourpagesinordertoexplainJavaScriptloadingthebestthat I can.
I have a fewdifferentwindowsopenheretohelpexplainontherighthandsideofmyscreen, I havetheCouncilformyWebpageopen, whichisgoingtobewalkingthedifferenttimesthatdifferenteventshappenandthenamethattheyhappen.
So, forexample, wehavethestartingofourHTMLparts, theendingofourHTMLpartsandthenthedocumentreadyfunctionsothatweknowwhenourdocumentisreadyandabletoberunonbyJavaskirtontheleft.
I havevisualstudiocodewith a smallHTMLpageand a verylargeHTMLpagetobeabletotesttheloadspeedsoftwodifferentsizedpages.
I alsohavetoJavascriptfilesthatarebothsmallandlargethatwecanusetotestwiththedifferentvotingmethodsandfinally, atthebottomofmyscreen, I justhavetheWebpagewith a buttonthatallowsmetogotothelargeorsmallversion.
Sorightnowwe'reonthesmallpage, have a click, fewother.
Butifwewantedtoload a largerdramascriptfileinstead, ifyousaythatyouknow, Alice, seethattheexecutionandfinishingofourdocumenttakesmuchlongerbecausewehavetowaitforthisnormalJavascripttobeloadedbeforewecanfinishparsingtherestofourHTMLdocument.
Inordertogetaroundthis, wecanuse a thinktank, forexample.
Soifweput a sinkinhereandwe'llsavethis, youseethatnowourdocumentisparsedandreadybefore a sinkinghiscodeactualgetsrun, andyoucankindofignorethetimeonthisasynchronousone, becauseit's justsomeinconsistencieswithmydownloadofmynetwork.
I wasgoingtousethelargejobscriptfilehere, andifyousaythatyouseethatexecutesbeforetheactualcontentisbeingfinished, Parson, becausewe'reusingnormalvoting, havewechangesto a sink, andwesaythatyouseethat I'm a sinkisactuallybeingexecutedbeforetheparsonisdonethistime, asopposedtoearlier.
Itwasbeingparsedafterwards.
Andifwecontinuetosaythis a fewtimes, youwillseethatinthiscaseare a sinkisactuallyexecutedafterourparsonofherhtmlSoyoucanseethatasynchronouscodewiththe A sinktagwillberunatdifferenttimes, dependingonthedownloadspeedofyournetwork.
JavaScriptandasynchronousismucheasiertousewhenyouhavesmallJavascriptfilesthatreallydon't dependonanythingelse, soyoudon't careiftheyloadbeforethepagesdonebeingparsedorafterthepagesdonebeingparsedforthemostpart, I wouldsayUsedifferbecauseit's morereliableandyoucouldalwaysknowwhereit's goingtobeexecuted.
Andyoudon't havetoworryaboutitbeingparsedbeforetheactualHTMLparsonisdone, whichcouldslowdowntheparsonifit's a largeexecutingfile.
Oneotherthingthat I didn't mentionisthatwhenyou'revoting a tagthenormalwaysuch a czthismostofthetime, peoplewillputthisscripttagattheveryendofthebodytagdownhereinordertoeliminatetheactualpausingoftheparsing.
I hopeyoulearnedsomethingabouthowyoucanloadjavascriptquickerandmakeyourpageboatquickerforyourenduserusing a sinkand D fortagsinsteadofjustusingthenormalscriptTechtoloadyourjobscript.