简体   繁体   English

需要帮助修改javascript代码段

[英]Need help modifying javascript snippet

Google optimizer includes the following snippet as part of their conversion code. Google优化器在其转换代码中包含以下代码段。 Unfortunately, the CMS we're using automatically converts the single quotes to ASCII (& #39;). 不幸的是,我们使用的CMS自动将单引号转换为ASCII(')。 I'm a novice with JS, but my understanding is that single quotes and double quotes are basically interchangeable. 我是JS的新手,但我的理解是单引号和双引号基本上可以互换。 However, it's not a straight swap as there are existing double quotes in the script. 但是,这不是直接交换,因为脚本中存在现有的双引号。 Is it possible to replace the single quotes with doubles in this script? 在此脚本中是否可以将双引号替换为单引号? If so, how do I escape the existing double quotes in the URL portion to keep the script functioning? 如果是这样,我如何在URL部分中转义现有的双引号以保持脚本正常运行?

<script type="text/javascript">
if(typeof(_gat)!='object')document.write('<sc'+'ript src="http'+
(document.location.protocol=='https:'?'s://ssl':'://www')+
'.google-analytics.com/ga.js"></sc'+'ript>')</script>

Yes, single and double quotes are interchangeable, you just need to escape the currently double quotes inside of the strings with \\" , and replace all the single quotes for double quotes: 是的,单引号和双引号是可互换的,您只需要用\\"转义字符串中当前的双引号,并将所有单引号替换为双引号即可:

<script type="text/javascript">
if(typeof(_gat)!="object")document.write("<sc"+"ript src=\"http"+
(document.location.protocol=="https:"?"s://ssl":"://www")+
".google-analytics.com/ga.js\"></sc"+"ript>");
</script>

Try this: 尝试这个:

<script type="text/javascript">
if(typeof(_gat)!="object")document.write("<sc"+"ript src=\"http"+
(document.location.protocol=="https:"?"s://ssl":"://www")+
".google-analytics.com/ga.js\"></sc"+"ript>")</script>
<script type="text/javascript">if(typeof(_gat)!="object") 
document.write("<sc"+"ript src=\"http"+
(document.location.protocol=="https:"?"s://ssl":"://www")+
".google-analytics.com/ga.js\"></sc"+"ript>")</script>
<script type="text/javascript">
if(typeof(_gat)!="object")document.write("<sc"+"ript src=http"+
(document.location.protocol=="https:"?"s://ssl":"://www")+
".google-analytics.com/ga.js></sc"+"ript>")</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM