Any value in double tag script?

I saw this in several parts of the code, and I didn’t want to “assume” that it didn’t matter, but this is a copy of the Google Analytics code:

<script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> try { var pageTracker = _gat._getTracker("UA-xxxxxx"); pageTracker._trackPageview(); } catch(err) {} </script> 

You will notice that there are two open / close script tags. Is there any reason why encapsulating code bits in two different script tags is useful? My first reaction would be to simply remove the redundancy.

+4
source share
4 answers

The first block writes the <script> to the page. I think that if the code was in one block, there would be no guarantee that the written <script> would be loaded before the second part of the code was executed.

Using two blocks, the recorded <script> will load (which contains the _gat object) before executing the second block.

+5
source

I think the two script blocks were created by different source modules (classes / objects / whatever).

0
source

The most likely reason a page has two script tags is because two separate system modules want to add javascript functionality to the page, but want to make sure that it inserts itself clean.

If they created the page as a single static page, one script tag would be fine. Since the page is probably dynamically generated, don't worry about it so much.

0
source

If an error occurs in the first tag of the script, the second will be executed.

If the second relies on the first, however (for example, if it uses the functions defined in the first), it can still fail. (Since the first one was not fully executed)

0
source

All Articles