需要帮助修改 javascript 片段
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,单引号和双引号是可以互换的,你只需要用
\"
转义字符串中当前的双引号,并将所有单引号替换为双引号: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:试试这个:
Try this: