Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2012
    Location
    South Carolina
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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>

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    You're not assigning the return value to anything.

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

  • #3
    New Coder
    Join Date
    Jun 2012
    Location
    South Carolina
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    New Coder
    Join Date
    Jun 2012
    Location
    South Carolina
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,643 Times in 4,605 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •