Search code examples
javascriptjqueryhtmltimingpageload

Many partial javascript sections


Does many javascript sections on different places affect page loading time? like

<html><script></script><body><script></script></body><script></script></html>

Also is there any affects while using referces or writing small script parts on html? And last, is it bad or good to write many codes on document load in page, will it affect page laod time while compiling all scripts on load


Solution

  • Obviously loading many different external JavaScript files will increase final load time.

    You can place all the <script> tags at the end of your document, right before the </body> so that the website loads before the scripts. If possible you could minify and combine your JS files, to decrease the number of HTTP requests as well.

    Page rendering will start as soon as all CSS is loaded and the DOM has been parsed, so this will make it seem like it loads faster.

    Try following some recommendations, like the Best Practices for Speeding Up Your Web Site, if you are concerned about page speed.

    Having JavaScript snippets inline with your HTML would not be a good idea. Try to keep your HTML clean, and use JavaScript with the appropriate selectors if you want to alter the DOM adding or removing any element.