CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Function is not removing special characters when called (http://www.codingforums.com/showthread.php?t=275354)

surfbird0713 10-05-2012 06:58 PM

Function is not removing special characters when called
 
I have a function to remove special characters but it's not working...not sure why. Can anyone help?
Code:

<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']);
                                      removeSplChars('Kitchen Tools', 'Textiles', 'Chef's Apron', '');
                         
                         
                           
                    _gaq.push(['_setCustomVar',
                                2, // This custom var is set to slot #2.
                                  'Kitchen Tools', // The top-level name for your online content categories.
                                  'Kitchen Tools|Textiles|Chef's Apron|', // Records value of breadcrumb name
                                  3 // Sets the scope to page-level.
                              ]);
                           
                               
                       

                          _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>


Logic Ali 10-05-2012 07:01 PM

You're not assigning the return value to anything.

Why are you passing multiple parameters when the function isn't coded to handle them?

surfbird0713 10-05-2012 07:08 PM

Unfortunately, the quick answer is because I don't understand the logic of JavaScript and I've been asked to "learn on the job" to help with a project at work.

Any chance you can help me write the code to make it work properly?

surfbird0713 10-05-2012 07:11 PM

Also, if it helps, this is being generated in a .JSPF file. I thought it would be easier to troubleshoot if the source from the browser was posted rather than the raw code from the JSPF file.

I have _trackEvent and custom variables set up throughout the site in different .JSP files (well, _trackEvent tags are in multiple .JSPs, only one custom variable) so I am trying to find a solution that will replace special characters whenever they come up in these tags on other pages too.

If there is a different solution, I'm all ears.

Old Pedant 10-05-2012 09:04 PM

This isn't even legal code:
Code:

removeSplChars('Kitchen Tools', 'Textiles', 'Chef's Apron', '');
I have marked in red the strings in that code. A string begins with a " or ' and ends with the next matching " or '.

If you want to embed a " or ' inside a string that is delimited by the same character, you must "escape" the character:
Code:

removeSplChars('Kitchen Tools', 'Textiles', 'Chef\'s Apron', '');
or you can use the other string delimiter:
Code:

removeSplChars('Kitchen Tools', 'Textiles', "Chef's Apron", '');
Anyway, I *suspect* that you simply have hour call to the function in the wrong place. Remove it from where you have it and try it here:
Code:

  _gaq.push(['_setCustomVar',
              2, // This custom var is set to slot #2.
              'Kitchen Tools', // The top-level name for your online content categories.
              removeSplChars('Kitchen Tools|Textiles|Chef\'s Apron|'), // Records value of breadcrumb name
              3 // Sets the scope to page-level.
            ]);

But that's purely a guess.

At the least, it should produce no errors.


All times are GMT +1. The time now is 01:57 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.