0

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.

Community
  • 1
  • 1
surfbird0713
  • 1,209
  • 2
  • 23
  • 45
  • Please clean up the code a bit before submission, and maybe only paste the parts that are relevant. – Mutahhir Oct 05 '12 at 19:30
  • Also, write/paste the error too – Mutahhir Oct 05 '12 at 19:33
  • if your [first question](http://stackoverflow.com/questions/12747470/javascript-remove-special-characters-string-not-working) does not get you the answer you were looking for, you should try to improve that question rather than reposting it as a new question. – jbabey Oct 05 '12 at 19:34
  • @jbabey, thank you for pointing that out. i've made a teeny bit of progress since that was posted so i thought i should create a new question rather than rephrase what people had already responded to? I guess it's better to just update the initial question. I will do that next to so as nut to clutter up the forum. – surfbird0713 Oct 05 '12 at 19:47

3 Answers3

0

Sorry, the previous answer was stupid. I apologize. It should've done the exact opposite.

One thing that i've noticed here is that you might be passing in multiple arguments to the removeSplChars method. The way the function is structured, it'll only fix the first string.

Also.. the 'fixed' string is being returned, whereas you're assuming it'll be fixed inline

you're doing something like:

removeSplChars(arg1, arg2, arg3, arg4);

and expecting that arg1, arg2, arg3, and arg4 are fixed, whereas, they're not. better is:

arg1 = removeSplChars(arg1);
arg2 = removeSplChars(arg2);
arg3 = removeSplChars(arg3);
arg4 = removeSplChars(arg4);

Ok, so this 'should' fix your problems. Have fun!

Mutahhir
  • 3,812
  • 3
  • 21
  • 26
  • There isn't an error message that's coming up - it's just not removing the special characters...I added the ' but it's still coming up. Editing my post to provide more details. – surfbird0713 Oct 05 '12 at 19:40
  • I updated the code, replacing arg1, arg2, arg3 with my statements. No luck. – surfbird0713 Oct 05 '12 at 20:14
  • I've been trying to edit this post so that it works, but I think there are more problems with this code than just javascript. I think you might not want to add the stuff within a string. I think you're calling the `removeSplChars` after you're really using the variables. I think you're not saving the results as I said to any javascript variable, instead writing stuff in the return variables area. I think you should read a bit more about what you're doing before you go ahead with this. – Mutahhir Oct 06 '12 at 02:56
0

It can be escaped like so:

<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> ${fn:escapeXml(myString)}

vector
  • 7,334
  • 8
  • 52
  • 80
0
String.prototype.unescapeHtml = function () {
    var temp = document.createElement("div");
    temp.innerHTML = this;
    var result = temp.childNodes[0].nodeValue;
    temp.removeChild(temp.firstChild);
    return result;
}

'&#039'.unescapeHtml().replace(/[^0-9a-zA-Z]/gi, '');

I got the unescapeHtml code from http://paulschreiber.com/blog/2008/09/20/javascript-how-to-unescape-html-entities/ and applied a regex to remove the special character after unescaping it.

Try it out. Just replace the string I let there for your variable.

André Silva
  • 1,149
  • 9
  • 30
  • This produces some results, I feel like I'm finally getting close! It still is not replacing the code within the gA tags though, but it is recognizing what I am looking for. However 'Chef's'.unescapeHtml.replace.... is still producing Chef's within the GA tag. It having trouble because the GA tag is also javascript? – surfbird0713 Oct 05 '12 at 20:56
  • also, is there a way to update the code so that it can be applied to values within javascript, not within an element (up there it applies to a div, i modified for an
  • ). if you look at the code i pasted above, there are no elements in there, it's all just javascript.
  • – surfbird0713 Oct 05 '12 at 20:59