...

View Full Version : Function is not removing special characters when called



surfbird0713
10-05-2012, 07:58 PM
I have a function to remove special characters but it's not working...not sure why. Can anyone help?

<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, 08: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, 08: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, 08: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, 10:04 PM
This isn't even legal 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:


removeSplChars('Kitchen Tools', 'Textiles', 'Chef\'s Apron', '');

or you can use the other string delimiter:


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:


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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum