Note that there are some explanatory texts on larger screens.

plurals
  1. POAnother way to remove special characters in JSP/JavaScript?
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/12747470/javascript-remove-special-characters-string-not-working">JavaScript Remove special characters string not working</a> </p> </blockquote> <p>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.</p> <p>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:</p> <pre><code> &lt;script type="text/javascript"&gt; function removeSplChars(inStr) { inStr = inStr.replace(/[^a-zA-Z0-9 ]/g, ""); return inStr; } var _gaq = _gaq || []; _gaq.push(['_setAccount', '&lt;c:out value="${profileId}"/&gt;']); &lt;c:choose&gt; &lt;c:when test="${(lastCmdName eq 'CategoryDisplay') or (lastCmdName eq 'ProductDisplay')}" &gt; _gaq.push(['_setCustomVar', 2, // This custom var is set to slot #2. '&lt;c:choose&gt;&lt;c:when test="${WCParam.source eq 'search'}"&gt;Search&lt;/c:when&gt;&lt;c:otherwise&gt;&lt;c:out value="${topCat}" /&gt;&lt;/c:otherwise&gt;&lt;/c:choose&gt;', // The top-level name for your online content categories. '&lt;c:choose&gt;&lt;c:when test="${WCParam.source eq 'search'}"&gt;Search &lt;c:out value="${WCParam.searchTerm}" /&gt;&lt;/c:when&gt;&lt;c:otherwise&gt;&lt;c:out value="${topCat}" /&gt;|&lt;c:out value="${subCatA}" /&gt;|&lt;c:out value="${subCatB}" /&gt;|&lt;c:out value="${subCatC}" /&gt;&lt;/c:otherwise&gt;&lt;/c:choose&gt;', // Records value of breadcrumb name 3 // Sets the scope to page-level. ]); &lt;/c:when&gt; &lt;c:otherwise&gt; &lt;/c:otherwise&gt; &lt;/c:choose&gt; removeSplChars('&lt;c:out value="${topCat}" /&gt;', '&lt;c:out value="${subCatA}" /&gt;', '&lt;c:out value="${subCatB}" /&gt;', '&lt;c:out value="${subCatC}" /&gt;'); _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); })(); &lt;/script&gt; </code></pre> <p>To give an example, in the line that is commented with <em>// Records value of breadcrumb name</em> Google is outputting the code like this:<br /><br /> [2,Kitchen Tools,Kitchen Tools|Textiles|Chef&amp; # 0 3 9 ; s Apron|,3]" (without spaces between the symbols of course)<br /><br /></p> <p>and what I am looking for is<br/> [2,Kitchen Tools,Kitchen Tools|Textiles|Chefs Apron|,3]"<br /><br /></p> <p>I have tried adding escapeXml="false" to the statement but that gives me an "unexpected identifier" error with no details.</p> <p>Now, I have also tried the code below. I would expect it to replace special characters with test, but nothing happens. </p> <pre><code> &lt;script type="text/javascript" src="&lt;c:out value="${jspStoreImgDir}javascript/Util.js"/&gt;"&gt; String.prototype.unescapeHtml = function () { var temp = document.createElement("li"); temp.innerHTML = this; var result = temp.childNodes[0].nodeValue; temp.removeChild(temp.firstChild); return result;} '&lt;c:out value="${catNameDisplay}" /&gt;'.unescapeHtml().replace((/[^a-zA-Z0-9 ]/g, 'test'); &lt;/script&gt; </code></pre> <p>Any help is much appreciated. I have been working on this all day and can't find a solution.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload