What is the advantage of using unescape on document.write to load javascript?

非 Y 不嫁゛ 提交于 2019-12-29 06:15:30

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/1224670/what-is-the-advantage-of-using-unescape-on-document-write-to-load-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!