Friday 15 February 2019

html - Should JavaScript go inside the for performance, rather than the ?




An IBM website talking about rapid web development here mentioned a useful skeleton HTML. In the template, the script inclusion is inside body rather than head. Is it a good practice? Isn't it better to put any library inside head instead?






Template












VS





Template











Answer




It is now standard for script tags to be included just before the closing of the body tag so that the script loading does not block the rest of the page loading.



  



With this, the user will not have to wait as long to see something on the page, then the javascript functionality is added.



However, there are more and more sites and "web apps" that are javascript/ajax heavy and may require the scripts to be loaded before anything is shown on the page anyway. That is less common, but that is a case where the script could be included in either location, since the visual result would be the same if javascript is responsible for creating/loading the content.




To verify: here is Google's recommendation: https://developers.google.com/apps-script/guides/html/best-practices#load_javascript_last



Also consider loading libraries from a CDN, so that you can take advantage of browser caching.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...