函数未被调用-尝试了JavaScript和JQuery
我正在尝试从谷歌分析标签中删除特殊字符。我有下面的脚本,我不明白为什么它不起作用。它是JSP和JavaScript的混合体,我对此知之甚少!在职学习函数未被调用-尝试了JavaScript和JQuery,javascript,function,jsp,analytics,Javascript,Function,Jsp,Analytics,我正在尝试从谷歌分析标签中删除特殊字符。我有下面的脚本,我不明白为什么它不起作用。它是JSP和JavaScript的混合体,我对此知之甚少!在职学习 <script type="text/javascript"> function removeSplChars(inStr) { inStr = inStr.replace(/[^a-zA-Z0-9 ]/g, "");
<script type="text/javascript">
function removeSplChars(inStr) {
inStr = inStr.replace(/[^a-zA-Z0-9 ]/g, "");
return inStr;
}
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>
removeSplChars(<c:out value="${topCat}" />, <c:out value="${subCatA}" />, <c:out value="${subCatB}" />, <c:out value="${subCatC}" />);
_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>
功能移除PLCHARS(仪表){
仪表=仪表更换(/[^a-zA-Z0-9]/g,”);
返回仪表;
}
var _gaq=_gaq | |[];
_gaq.push([''设置帐户','');
_gaq.push([''u setCustomVar',
2,//此自定义变量设置为插槽#2。
'搜索',//联机内容类别的顶级名称。
'Search | | |',//记录面包屑名称的值
3//将范围设置为页面级别。
]);
移除plchars(,);
_gaq.push([''u trackPageview']);
(功能(){
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);
})();
我是不是遗漏了什么?我在控制台中收到一条“意外令牌;”错误消息。我尝试在$(document.ready(function(){})中调用removeSplChars
无济于事
我查看了以下帖子,但这些建议对我没有帮助:
我在这里还有一个帖子:
多谢各位
通过查看源代码添加代码:
<script type="text/javascript">
function removeSplChars(inStr) {
inStr = inStr.replace(/[^a-zA-Z0-9 ]/g, "");
return inStr;
}
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-33021136-1']);
_gaq.push(['_setCustomVar',
2, // This custom var is set to slot #2.
'Dine & Entertain', // The top-level name for your online content categories.
'Dine & Entertain|Bowls & Platters||', // Records value of breadcrumb name
3 // Sets the scope to page-level.
]);
$(document).ready(function() {
removeSplChars(Dine & Entertain, Bowls & Platters, , );
});
_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>
功能移除PLCHARS(仪表){
仪表=仪表更换(/[^a-zA-Z0-9]/g,”);
返回仪表;
}
var _gaq=_gaq | |[];
_gaq.push([''设置帐户','UA-3302136-1']);
_gaq.push([''u setCustomVar',
2,//此自定义变量设置为插槽#2。
“Dine&;Entertainment”,//您的在线内容类别的顶级名称。
“用餐和娱乐|碗和盘| |”,//记录面包屑名称的值
3//将范围设置为页面级别。
]);
$(文档).ready(函数(){
移除餐具(餐饮和娱乐、碗和盘子);
});
_gaq.push([''u trackPageview']);
(功能(){
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);
})();
传递给removeSplChars
的字符串没有括在引号中,因此会出现错误。如果可以的话,在JSON编码后传递变量更可靠。或者,您可以将“
放在它的周围,并希望得到最好的结果(如果您已经将”
编码为输入中的“
,它应该可以正常工作)
此外,这将解决空参数的问题:空字符串将作为参数传递。最好在浏览器中查看生成的代码[aka view source]而不是服务器端标记。@epascarello,谢谢,我已经更新了我的帖子来展示这些代码。如果您能提供任何帮助,我将不胜感激。谢谢。我添加了引号,不再出现错误,但我仍然在字符串中看到特殊字符。这给了我什么?这让我发疯了。你知道它为什么不能工作吗我怎么加上引号了?