在document.write上使用unescape来加载javascript有什么好处? [英] What is the advantage of using unescape on document.write to load javascript?

查看:123
本文介绍了在document.write上使用unescape来加载javascript有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < script type =>您必须添加的代码才能用Google Analytics分析网页。 文本/ 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>
尝试{
var pageTracker = _gat._getTracker(UA-XXXXX);
pageTracker._trackPageview();
} catch(err){}< / script>

做这些行的优点是:

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

与这些行相比:

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

我编写了一些类似的代码(通过document write加载JavaScript),但它不使用unescape,我想知道是否应该遵循google-analytics的例子。 解决方案

这意味着代码将在XML / XHTML和HTML,而不必混淆CDATA


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.com/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.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

versus these line:

document.write("<script src='" + gaJsHost + "google-analytics.com/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.

解决方案

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

这篇关于在document.write上使用unescape来加载javascript有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆