Requires a script to split and assign jQuery values

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

#&first=1&second=2

can anyone help with a script that will split and assign these values ​​to another variable so that it looks like

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

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

thank

+3
source share
3 answers

You can 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];
}

See an example on jsfiddle: http://jsfiddle.net/WxnJq/

+3
source

Here's the one that breaks the GET variables into key / value pairs, and I believe you are after this: http://snipplr.com/view/12208/javascript-url-parser/

, parseURI, javascript, URL- GET .

, , .

+3

JQuery has no built-in way of working with querystrings, so I use a plugin for this. You can get it here , works great.

+1
source

All Articles