Why create script tag via Javascript, instead of using defer or async script tag attributes?

682 views Asked by At

Some Web applications (I'm thinking about Disqus and LiveFyre) create <script> tags via Javascript, and via Javascript specify that the new scripts be loaded asynchronously. Why do they create the tags via Javascript? Instead of simply doing:

<script src="..." async>

An example:

This is how Disqus instructs website owners to load comments:

<script type="text/javascript">
    var disqus_shortname = ...
    (function() {
        var s = document.createElement('script');
        s.type = 'text/javascript';
        s.async = true;
        s.src = '//' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] ||
           document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
</script>

And the src = ...disqus.com/embed.js address is simply a redirect to a static script on another Disqus server, apparently independent of the disqus_shortname.

Why not instead tell people to use this piece of code:

<script>
    var disqus_shortname = ...
</script>
<script src="http://direct-address-to-the-embed.js-script" async>

Or even simpler, just one line:

<script src="http://the_disqus_shortname.disqus.com/embed.js" async>

?

(P.S. I added one answer below. Please do add other answers too :-))

3

There are 3 answers

0
KajMagnus On

I can think of one uncertain (no. 1) and two likely reasons (no. 2 and 3):

[Edit] But reasons 2 and 3 are moot points — Disqus could simply do this despite of my reasons 2 and 3:

<script src="http://the_disqus_shortname.disqus.com/embed.js" async>

[/Editi]

  1. (Perhaps old browsers that doesn't understand the async attribute completely ignore the whole <script ... async> tag? Instead of ignoring only async and loading the script synchronously?)

  2. Perhaps Disqus would like to be able to genereate the embedded script dynamically, or redirect to different scripts, depending on website settings (although it seems as if Disqus currently always redirects to the same embed.js script always). Without having users reconfigure their Disqus code.

  3. Using a redirect allows Disqus to tell the browser to cache the-scripts-that-is-redirected-to for a long amount of time, but at the same time makes it possible to quickly redirect to another script. At the cost of 1 redirect per download / fetch-from-browser-cache. This was suggested here: https://stackoverflow.com/a/10098250/694469

1
HankMoody On

My guess is that Disqus (and other services) want to ensure that their scripts doesn't slow down loading of user sites whenever you put their script (probably many CMSes put scripts in a head element).

If you always put your scripts at the end of a body element then writing short version may be optimal:

    ...
    <script>var disqus_shortname = ...</script>
    <script src="//shortname.disqus.com/embed.js" async>
</body>

I'm not sure though!

0
in need of help On

This is a bit old but still relevant, the main reason we do it is because some tools, such as google-tag-manager, don't accept scripts with certain properties set on them. e.g. if you try to set the refferer-policy to 'unsafe-url' then google tag manager will just ignore your request to add the <script> tag.