Possible Duplicate:
JavaScript Remove special characters string not working
I am trying to remove special characters that are in dynamically populated values being sent to Google Analytics; the ' in some products like "Matt's" are causing JS errors.
This is especially challenging for me because I don't really understand JavaScript or JSP. I have the following code written, but it does not achieve the desired results. Is there another way to do this? I have to modify this for Custom Variables and for _trackEvent calls directly in the anchor tags. Below is the code for custom variables:
<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>
To give an example, in the line that is commented with // Records value of breadcrumb name Google is outputting the code like this:
[2,Kitchen Tools,Kitchen Tools|Textiles|Chef& # 0 3 9 ; s Apron|,3]" (without spaces between the symbols of course)
and what I am looking for is
[2,Kitchen Tools,Kitchen Tools|Textiles|Chefs Apron|,3]"
I have tried adding escapeXml="false" to the statement but that gives me an "unexpected identifier" error with no details.
Now, I have also tried the code below. I would expect it to replace special characters with test, but nothing happens.
<script type="text/javascript" src="<c:out value="${jspStoreImgDir}javascript/Util.js"/>">
String.prototype.unescapeHtml = function () {
var temp = document.createElement("li");
temp.innerHTML = this;
var result = temp.childNodes[0].nodeValue;
temp.removeChild(temp.firstChild);
return result;}
'<c:out value="${catNameDisplay}" />'.unescapeHtml().replace((/[^a-zA-Z0-9 ]/g, 'test');
</script>
Any help is much appreciated. I have been working on this all day and can't find a solution.