Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two different ways of putting the script at the bottom - what are the differences?

What are the differences between the two solutions below ? In particular, is there a good reason to favour 2 over 1. (note: Please assume the name of the script to load is known. The question is just about if there is value in creating a minimal script to load a script in the given situation )

1 - Script At The Bottom

<html>
<body>
...
...
<script src='myScript.js'></script>
</body>
</html>

2 - Script at the bottom loads external script

<html>
<body>
...
...
<script>
    // minimal script to load another script
    var script = document.createElement('script');
    script.src = 'myScript.js'
    document.body.appendChild(script);
</script>
</body>
</html>
like image 802
Zo72 Avatar asked Mar 31 '14 09:03

Zo72


People also ask

What's the difference between putting script in head and body?

JavaScript in head: A JavaScript function is placed inside the head section of an HTML page and the function is invoked when a button is clicked. JavaScript in body: A JavaScript function is placed inside the body section of an HTML page and the function is invoked when a button is clicked.

Why scripts are placed at bottom of the Page?

When you place your JavaScript at the bottom of your HTML body, it gives the HTML time to load before any of the JavaScript loads, which can prevent errors, and speed up website response time.

Why is it usually best to put the script tag at the very bottom of the Page just before the closing </ body tag?

Antiquated recommendation. The old approach to solving this problem was to put <script> tags at the bottom of your <body> , because this ensures the parser isn't blocked until the very end.


1 Answers

One important feature of the second one is that it allows the browser to finish parsing the page immediately, without waiting for the script to load. That's because the first example allows the script to use document.write to change the parsing state around the <script> tag, while the second one doesn't.

Now, we know that it's at the bottom of the page so that there isn't any important content left to parse, but this is still an important difference. It's not until parsing is done that the browser fires the popular DOMContentLoaded event. In method 1, the event fires after the script loads and executes. In method 2, the event fires before the script starts loading.

Here are some examples. In these demos, a DOMContentLoaded listener changes the background color to yellow. We try to load a script that takes 3 seconds to load.

  1. http://jsfiddle.net/35ccs/
  2. http://jsfiddle.net/VtwUV/

(edit: Maybe jsfiddle wasn't the best place to host these demos. It doesn't show the result until the slow-loading script loads. Be sure to click Run again once it loads, to see what happens.)

Pick the approach that's best for your application. If you know you need the script to run before DOMContentLoaded, go with method 1. Otherwise, method 2 is pretty good in most cases.

like image 158
guest Avatar answered Nov 10 '22 01:11

guest