需要帮助修改 javascript 片段

发布于 2024-08-09 15:56:02 字数 447 浏览 2 评论 0原文

Google 优化器包含以下代码片段作为其转换代码的一部分。不幸的是,我们使用的 CMS 会自动将单引号转换为 ASCII (& #39;)。我是JS新手,但我的理解是单引号和双引号基本上是可以互换的。但是,这不是直接交换,因为脚本中存在双引号。是否可以在此脚本中用双引号替换单引号?如果是这样,如何转义 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>

Google optimizer includes the following snippet as part of their conversion code. Unfortunately, the CMS we're using automatically converts the single quotes to ASCII (& #39;). I'm a novice with JS, but my understanding is that single quotes and double quotes are basically interchangeable. 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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南渊 2024-08-16 15:56:02

是的,单引号和双引号是可以互换的,你只需要用 \" 转义字符串中当前的双引号,并将所有单引号替换为双引号:

<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>
不离久伴 2024-08-16 15:56:02

试试这个:

<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>
谁人与我共长歌 2024-08-16 15:56:02
<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>
独闯女儿国 2024-08-16 15:56:02
<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文