0

I've got a chunk of code that is doing something really strange.

function getQuerystring(key, default_){

  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,'\\\[').replace(/[\]]/,'\\\]');
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

var isThankyou = getQuerystring('CONFIRMATION');

This function checks the URL for a parameter (in this case CONFIRMATION). From what I can tell everything is correct, but when the browser loads the code it throws an error in the console.

Uncaught SyntaxError: Invalid regular expression: missing /

Ultimately this chunk of code is used to determine if a user is on the confirmation page of the URL. If they are then it triggers some google analytics code to track ecommerce purchase information in GA.

The line that is giving me trouble is:

key = key.replace(/[\[]/,'\\\[').replace(/[\]]/,'\\\]');

When I load the page in the browser and look at the source this is what shows up:

key = key.replace(/[\[]/,'\\\[').replace(/[\/,'\\\]');

with two ]] showing up just before

Here's the weird thing. When I duplicate the line commenting out the first one, the error doesn't get thrown (the ]] still shows up however):

//key = key.replace(/[\[]/,'\\\[').replace(/[\]]/,'\\\]');
  key = key.replace(/[\[]/,'\\\[').replace(/[\]]/,'\\\]');

I've tried everything I can think of with no success, I've even tried unchaining the replace method in case that was causing the issue.

key = key.replace(/[\[]/,'\\\[');
  key = key.replace(/[\]]/,'\\\]');

Which results in the following in the browser source:

key = key.replace(/[\[]/,'\\\[');
  key = key.replace(/[\/,'\\\]');

In any case, the issue seems to be with the second .replace not escaping the one ] or even keeping the two ]] for some reason.

I've also tried erasing cache, cookies, and turning off all plugins. I've tested in Chrome, Firefox, and Safari with the same results on all 3 browswers.

Here is the page that you can see the full block of code (last in the head): https://secure2.convio.net/ccod/site/Ecommerce?store_id=2841

4
  • 2
    tldr; what is your input, what do you want the output to be? Commented Mar 9, 2013 at 0:23
  • Ultimately this chunk of code is used to determine if a user is on the confirmation page of the URL. If they are then it triggers some google analytics code to track ecommerce purchase information in GA. Commented Mar 9, 2013 at 0:25
  • 1
    No I mean literally, what is your input? What are some possible values of window.location.href? Commented Mar 9, 2013 at 0:25
  • secure2.convio.net/ccod/site/Ecommerce?store_id=2841 , secure2.convio.net/ccod/site/EcommerceCheckout/… , is that what you mean? I apologize if I still didn't understand the question. Commented Mar 9, 2013 at 0:28

2 Answers 2

1

Your code looks a bit complex. Here's something simpler:

function getQueryString(key) {
    var index, url, parts, i, subparts;
    url = window.location.href;
    index = url.indexOf("?");
    if (index === -1) {
        return null;
    }
    url = url.substr(index);
    parts = url.split("&");
    for (i = 0; i < parts.length; i += 1) {
        subparts = parts[i].split("=");
        if (subparts[0] === key) {
            return subparts[1];
        }
    }
    return null;
}

(untested)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, let me try this and see if it works. I don't really care how I get the URL parameter, just as long as I get it :)
0

This causes an error: key = key.replace(/[\/,'\\\]') because you escape the forward slash at the end of the regex with a back-slash like this: /[\/

Try this instead key = key.replace(/[\\/,'\\\]')

3 Comments

Yeah, I understand that is why the error is thrown, what I don't understand is why when the original code is key = key.replace(/[]]/,'\\]'); it is rendered in the browser with what you have above. Really frustrating.
How are you rendering the javascript, with php? Or are you just using plain html?
Yeah, straight html. I literally am pasting directly into the page editor. Though the more I think about it, I'm wondering if it's the system (online donation platform) that could be causing the problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.