我试图删除可能出现在我的Google Analytics代码中的特殊字符,因为特殊字符似乎在某些版本的IE中导致脚本错误。我有这样的功能:JavaScript删除特殊字符字符串不工作
function removeSplChars(inStr) {
inStr = inStr.replace(/[^a-zA-Z0-9 ]/g, "");
return inStr;
}
而且是当前工作的GA代码:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '<c:out value="${profileId}"/>']);
<c:choose>
<c:when test="${(lastCmdName eq 'CategoryDisplay') or (lastCmdName eq 'ProductDisplay')}" >
_gaq.push(['_setCustomVar',
2, // This custom var is set to slot #2.
'<c:choose><c:when test="${WCParam.source eq 'search'}">Search</c:when><c:otherwise><c:out value="${topCat}" /></c:otherwise></c:choose>', // The top-level name for your online content categories.
'<c:choose><c:when test="${WCParam.source eq 'search'}">Search <c:out value="${WCParam.searchTerm}" /></c:when><c:otherwise><c:out value="${topCat}" />|<c:out value="${subCatA}" />|<c:out value="${subCatB}" />|<c:out value="${subCatC}" /></c:otherwise></c:choose>', // Records value of breadcrumb name
3 // Sets the scope to page-level.
]);
</c:when>
<c:otherwise>
</c:otherwise>
</c:choose>
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
但是,当我把代码内的功能,我仍然看到特殊字符在Chrome调试器来了。例如,当我拉起一个名为“Matt's”的产品的页面时,它显示为Matt ' s。我想要的是Matts。我们有其他产品名称与&符号和其他特殊字符,所以我只想让A-Z和数字(盖帽/没有盖帽都OK)
任何意见,将不胜感激。我已经看了看所以下面的职位,但到目前为止还没有发现任何东西,可以帮助我,使这项工作:
How to handle (® ´ © ¿ ¡ ° À) special characters in javascript?
javascript regexp remove all special characters
Remove all special characters except space from a string using JavaScript
我是新来的JSP和JavaScript,所以我相信我”没有把代码放在正确的位置,或者我需要在页面上添加其他内容?我试过在()中放置removeSplChars函数或添加一个;并没有运气。不幸的是,我必须在工作中学习这一点,所以我必须专注于完成我已经完成的任务,而不是花时间真正理解语言的逻辑/语法。
向我们展示您调用'removeSplChars'的代码。 – some
您可以用三元表达式替换内嵌的'c:choose'块。例如:'$ {WCParam.source eq'search'? '搜索':'topCat'}' –
@some,这是整个问题,我不会在任何地方调用它。我是一个JavaScript的总noobot :(我将研究如何调用一个函数,看看我找到了什么,谢谢 – surfbird0713