Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-05-2012, 06:58 PM   PM User | #1
surfbird0713
New Coder

 
Join Date: Jun 2012
Location: South Carolina
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
surfbird0713 is an unknown quantity at this point
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>
surfbird0713 is offline   Reply With Quote
Old 10-05-2012, 07:01 PM   PM User | #2
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 959
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
You're not assigning the return value to anything.

Why are you passing multiple parameters when the function isn't coded to handle them?
Logic Ali is offline   Reply With Quote
Old 10-05-2012, 07:08 PM   PM User | #3
surfbird0713
New Coder

 
Join Date: Jun 2012
Location: South Carolina
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
surfbird0713 is an unknown quantity at this point
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 is offline   Reply With Quote
Old 10-05-2012, 07:11 PM   PM User | #4
surfbird0713
New Coder

 
Join Date: Jun 2012
Location: South Carolina
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
surfbird0713 is an unknown quantity at this point
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.
surfbird0713 is offline   Reply With Quote
Old 10-05-2012, 09:04 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
analytics, function, javascript

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:51 AM.


Advertisement
Log in to turn off these ads.