我試圖刪除可能出現在我的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