javascript - Jquery get window.location.search tag in array - Stack Overflow

Let's say i have this address: http:**test.php?alfa=1&beta=2I know that i can get ?alfa=1&a

Let's say i have this address: http://**/test.php?alfa=1&beta=2

I know that i can get ?alfa=1&beta=2 using the search tag in window location.... but is there any way to split the result into 2 strings, in this case, first would be ?alfa=1 and the second one &beta=2 (or just beta=2) using JQuery?

Let's say i have this address: http://**/test.php?alfa=1&beta=2

I know that i can get ?alfa=1&beta=2 using the search tag in window location.... but is there any way to split the result into 2 strings, in this case, first would be ?alfa=1 and the second one &beta=2 (or just beta=2) using JQuery?

Share Improve this question asked Oct 18, 2013 at 14:53 SpiderLinkedSpiderLinked 3733 gold badges6 silver badges14 bronze badges 2
  • var parameters = url.split("?")[1].split("&"); then that would set parameter[0] = "alfa=1"....parameter[1] = "beta=2" – em_ Commented Oct 18, 2013 at 14:57
  • Thank you..i knew there was a split tag in java...but i did not think that it might also be one in jquery – SpiderLinked Commented Oct 18, 2013 at 14:58
Add a ment  | 

6 Answers 6

Reset to default 3
// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

Not my work. Found it here First hit on Google.

you can use Split method,

sample- var str = "How are you doing today?"; var n = str.split(" ");

will give you array How,are,you,doing,today?.

for your problem you can spit text using '?' keyword first and next by '&' key...

there is a plugin jQuery BBQ: http://benalman./projects/jquery-bbq-plugin/

You can use the following function, but it returns an object and not an array

var params = $.deparam.querystring();
console.log(params);

Object {alfa: "1", beta: "2"}

URL: //somesite./?v1=1&v2=2&v3=3

$(function () {
  var in_url = [];
  var in_url_str = window.location.search.replace('?','').split('&');
  $.each(in_url_str,function (kay,val) {
    var v = val.split('=');
    in_url[v[0]]=v[1];
  });
  console.log(in_url);
});

in console

[v1: "1", v2: "2", v3: "3"]
v1:"1"
v2:"2"
v3:"3"
let params = [];
let query = decodeURIComponent(window.location.search.replace('?','')).split('&');
query.map((row)=>{
    let r = row.split("=");
    params[r[0]] = r[1];
})

To get a Json object with keys.

JSON.parse('{' + window.location.search.slice(1).split('&').map(x => { y = x.split('='); return '"' + y[0] + '":' + y[1]}).join(',')  + '}')

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744873046a4598377.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信