Why is Google canceling its Google Analytics tracking code?

Just slide off my JavaScript training wheels.

Why unescape Google choose unescape document.write line in Part 1 below?

Why don't they just write it like that? Maybe unescape is required for older browser compatibility?

 document.write('<script src="' + gaJsHost + 'google-analytics.com/ga.js" type="text/javascript"></script>'); 

For reference, the entire Google Analytics tracking code is as follows:

Part 1:

 <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> 

Part 2:

 <script type="text/javascript"> try { var pageTracker = _gat._getTracker("UA-0000000-0"); pageTracker._trackPageview(); } catch(err){} </script> 

I understand what the rest of the code does, is just interested in the unescape part.

Edit

The bottom line says unescape . Voted to close this question because it is a duplicate (see Answer marked as correct).

+7
javascript google-analytics
source share
4 answers

This means that the code will work in XML / XHTML and HTML without having to bind to CDATA

Please see: https://stackoverflow.com>

+5
source share

My understanding is that </script > is found even inside quotation marks "</script>" parser misunderstood that its final end is a script, so they cannot do like "</script>"

And Google wants to make sure variables like pageTracker are set before google-analytics.com/*.js loaded, so unescaping %3Cscript and %3E%3C/script%3E is just the way for them.

just my 2 cents, sorry if I say wrong.

+2
source share

Writing directly to a document without using '<' or '>' means that you do not need to avoid them in document formats that interpret them literally. Otherwise, the correct interpretation is that the <script> tags begin inside the line, which is not desirable.

Also note that there is an error in the proposed alternative code (you missed the quotation mark after the end of the src attribute).

0
source share

I think that:

 document.wrIte('<script src="'" 

will also complete HTML validation. Interestingly, it also breaks the preview in this comment field :)

0
source share

All Articles