最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google analytics - What is the advantage of using unescape on document.write to load javascript? - Stack Overflow

programmeradmin3浏览0评论

The code that you have to add to track a web page with google analytics looks like:

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

What's the advantage of doing these line:

document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics/ga.js' type='text/javascript'%3E%3C/script%3E"));

versus these line:

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

I wrote some code that does something similar (load javascript "via" document write) but it does not use unescape and I am wondering if I should follow the google-analytics example.

The code that you have to add to track a web page with google analytics looks like:

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

What's the advantage of doing these line:

document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics./ga.js' type='text/javascript'%3E%3C/script%3E"));

versus these line:

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

I wrote some code that does something similar (load javascript "via" document write) but it does not use unescape and I am wondering if I should follow the google-analytics example.

Share Improve this question edited Dec 27, 2013 at 17:58 fgui asked Aug 3, 2009 at 21:11 fguifgui 1,5741 gold badge13 silver badges14 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 14

It means the code will work in XML / XHTML and HTML without having to mess with CDATA

Well, one advantage is that it means you don't have to worry about quotes within the script being loaded interfering with quotes in your script that's doing the loading (since the ones in the loaded script can be escaped).

发布评论

评论列表(0)

  1. 暂无评论