I am using some URL parameters in my jQuery and have come across a bug. Since JS is not my primary (or even secondary if I'm honest) language I am at a loss to know why.
I have the following function.
- function urlParam(name){
- var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
- if (results == null) {
- return null;
- } else {
- return results[1] || 0;
- }
- };
I then call this with the following lines (much reduced for debugging)...
- var urlProduct = urlParam('product');
- if (urlProduct != null) {
- alert(urlProduct);
- };
if I go to
http://mysite/dir/?product=xxx then all seems to work unless the string xxx contains a lower-case a, m or p. any of these 3 letters will interrupt the string so...
"?product=123a456" will be "123"
but
"?product=123b456" will correctly return "123b456"
As far as I can tell it is only these 3 letters, upper-case works fine so 123A456 will work. It also make no diference where in the string they occur.
I assume the issue is being caused by some faulty regex, specifically in this bit...
- '=([^&#]*)'
but can someone with more knowledge than help me fix it?
Thanks.