javascript - Advantages of script tag before closing body tag when using events -



javascript - Advantages of script tag before closing body tag when using events -

this question has reply here:

where best place set <script> tags in html markup? 13 answers

these days seems people recommend placing script tag before closing body tag such..

class="lang-html prettyprint-override"> <script src="//javascript.js"></script> </body> </html>

if running script immediately, thing because of dom has loaded. however, if using onload or domcontentloaded events phone call main script? in case, seems create no difference if placing script tag in document head, since code not execute anyways until dom loads.

when using onload or domcontentloaded events run script, there advantage putting script tag above closing body tag vs head?

on other hand, if don't want script execute until dom has loaded, why not place script tag before closing body tag , run without events whatsoever. seems save few lines of code while providing same functionality.

i think, run js code in littile diffrent context ,because tag end of body (inline source) "top-level" code domcontentloaded handler in event listener function. domcontentloaded executed after script tag execution. see littile experience domcontentloaded useful in cases when cant command markup.

javascript html dom javascript-events onload

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -