2009-10-27 155 views
0

Google优化程序包含以下片段作为其转换代码的一部分。不幸的是,我们使用的CMS自动将单引号转换为ASCII(')。我是JS的新手,但我的理解是单引号和双引号基本上是可以互换的。但是,由于脚本中存在双引号,因此它不是直接交换。在脚本中是否可以用双打替换单引号?如果是这样,我如何转义URL部分中的现有双引号以保持脚本正常运行?需要帮助修改javascript代码段

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

回答

1

是,单引号和双引号是可以互换的,你只需要与\"逃脱字符串里的当前双引号,并取代所有的单引号双引号:

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

尝试这样的:

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