0

I have jQuery script that takes a URL parameter and adds the value to an input box.

When the URL is the form of www.mysite.com/page/?number=10 everything works fine. When the url includes .php at the end of the page than the script doesn't work, e.g. www.mysite.com/page.php?number=10.

I need for the script to work with .php is used.

Here is the script:

  jQuery(document).ready(function() {
    (function($) {
        jQuery.QueryString = (function(a) {
            if (a == "") return {};
            var b = {};
            for (var i = 0; i < a.length; ++i)
            {
                var p=a[i].split('=');
                if (p.length != 2) continue;
                b[p[0]] = decodeURIComponent(p[1].replace(/+/g, " "));
            }
            return b;
        })(window.location.search.substr(1).split('&'))
    })(jQuery);

    jQuery.QueryString["number"];

    jQuery("#entry_2081224724").val(jQuery.QueryString["number"]);

  });
3
  • What do you mean "doesn't work"? Have you debugged to see where the issue is? Commented Oct 9, 2013 at 20:36
  • It doesn't work as in the parameter value does not get added to the input field. In Chrome, I receive this error "Uncaught SyntaxError: Invalid regular expression: /+/: Nothing to repeat" but I don't know how to fix the expression. Commented Oct 9, 2013 at 20:40
  • Your regular expression is currently containing a quantifier + that means "1 or more" of the expression preceding it. But you mean the + literally, so you should escape it like so \+. Commented Oct 9, 2013 at 20:43

1 Answer 1

1

Read this:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

Your issue is, the regular expression character + gives an instruction to repeat the character that precedes it one or more times. The syntax error your getting is telling you that you haven't given it a character to repeat.

Are you trying to replace all + characters? If so, you're going to need to escape it with \.

Are you trying to say "one or more / characters"? Again, you'll need to escape that too, because / denotes the beginning of a regular expression.

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

2 Comments

I believe the OP is looking to replace the plus signs in the URL with spaces. So it should probably be /\+/.
Yes, this is correct. Thanks for the help and the link to read up on regular expressions. That's definitely an area I don't experience in. Thanks again.

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.