【发布时间】:2012-09-26 19:06:59
【问题描述】:
我正在尝试从 Google Analytics 标记中删除特殊字符。我有以下脚本,但我不知道为什么它不起作用。它是 JSP 和 JavaScript 的混合体,我对此知之甚少!在工作中学习...
<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>
我是否遗漏了一些 () 或某处的东西?我得到一个“意外的令牌;”控制台中的错误消息。我尝试将 removeSplChars 调用放在 $(document).ready(function() {});
无济于事。
我检查了以下帖子,但这些建议对我没有帮助: Function is not Called javascript function not getting called
我这里还有一个帖子:JavaScript Remove special characters string not working
谢谢。
通过查看源代码添加代码:
<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>
【问题讨论】:
-
最好在浏览器中查看生成的代码 [aka view source] 而不是服务器端标记。
-
@epascarello,谢谢,我已更新我的帖子以显示该代码。如果您能提供任何帮助,我将不胜感激。
标签: javascript function jsp analytics