3

I am trying to separate and reassign values in a variable. What I have is

#&first=1&second=2

can anyone help with a script that will separate and assign this values to another variable so it would be like

var first= val.(first);
var second= val.(second);

I am new to jquery so I am not even sure if I am using the correct syntax.

Thanks

3 Answers 3

3

You could do something like this:

var val = "#&first=1&second=2";

var first = gup(val, "first");
var second = gup(val, "second");


function gup(str, name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(str);
    if (results == null) return "";
    else return results[1];
}

Checkout the example on jsfiddle: http://jsfiddle.net/WxnJq/

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

Comments

3

Here's the one that breaks down the GET variables in to key/value pairs, which I believe is what you're after: http://snipplr.com/view/12208/javascript-url-parser/

Also, you can take a look at parseURI, a javascript function that can dissect a URL with GET parameters and grab sections.

Sorry for being quick to post, I thought I found the right function the first time.

2 Comments

Actually, hold on--you want the GET variables. This just breaks sections. Bear with me, looking for the right function...
Hey Brad, works but a little to long. @kmb385 post was perfect for what I needed. Thanks
1

JQuery has no builtin way of dealing with querystrings, so I use a plugin for this. You can get it here, works great.

Comments

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.