Where to place JavaScript in an HTML file?

前端 未结 12 2212
萌比男神i
萌比男神i 2020-11-22 04:30

Say I have a fairly hefty JavaScript file, packed down to roughly 100kb or so. By file I mean it’s an external file that would be linked in via

相关标签:
12条回答
  • 2020-11-22 05:08

    The best place for it is just before you need it and no sooner.

    Also, depending on your users' physical location, using a service like Amazon's S3 service may help users download it from a server physically closer to them than your server.

    Is your js script a commonly used lib like jQuery or prototype? If so, there are a number of companies, like Google and Yahoo, that have tools to provide these files for you on a distributed network.

    0 讨论(0)
  • 2020-11-22 05:09

    The answer is depends how you are using the objects of javascript. As already pointed loading the javascript files at footer rather than header certainly improves the performance but care should be taken that the objects which are used are initialized later than they are loaded at footer. One more way is load the 'js' files placed in folder which will be available to all the files.

    0 讨论(0)
  • 2020-11-22 05:11

    Like others have said, it should most likely go in an external file. I prefer to include such files at the end of the <head />. This method is more human friendly than machine friendly, but that way I always know where the JS is. It is just not as readable to include script files anywhere else (imho).

    I you really need to squeeze out every last ms then you probably should do what Yahoo says.

    0 讨论(0)
  • 2020-11-22 05:12

    The answer to the question depends. There are 2 scenarios in this situation and you'll need to make a choice based on your appropriate scenario.

    Scenario 1 - Critical script / Must needed script

    In case the script you are using is important to load the website, it is recommended to be placed at the top of your HTML document i.e, <head>. Some examples include - application code, bootstrap, fonts, etc.

    Scenario 2 - Less important / analytics scripts

    There are also scripts used which do not affect the website's view. Such scripts are recommended to be loaded after all the important segments are loaded. And the answer to that will be bottom of the document i.e, bottom of your <body> before the closing tag. Some examples include - Google analytics, hotjar, etc.

    Bonus - async / defer

    You can also tell the browsers that the script loading can be done simultaneously with others and can be loaded based on the browser's choice using a defer / async argument in the script code.

    eg. <script async src="script.js"></script>

    0 讨论(0)
  • 2020-11-22 05:13

    Putting the javascript at the top would seem neater, but functionally, its better to go after the HTML. That way, your javascript won't run and try to reference HTML elements before they are loaded. This sort of problem often only becomes apparent when you load the page over an actual internet connection, especially a slow one.

    You could also try to dynamically load the javascript by adding a header element from other javascript code, although that only makes sense if you aren't using all of the code all the time.

    0 讨论(0)
  • 2020-11-22 05:16

    Your javascript links can got either in the head or at the end of the body tag, it is true that performance improves by putting the link at the end of your body tag, but unless performance is an issue, placing them in the head is nicer for people to read and you know where the links are located and can reference them easier.

    0 讨论(0)
提交回复
热议问题