最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Loop through URL GET variables with javascript - Stack Overflow

programmeradmin1浏览0评论

I know you can obtain an URL variable by calling getUrlVars()["id"], however is there a way to get all (an unknown number of) variables in the URL? For a few reasons I am only allowed to do this on client side.

I know you can obtain an URL variable by calling getUrlVars()["id"], however is there a way to get all (an unknown number of) variables in the URL? For a few reasons I am only allowed to do this on client side.

Share Improve this question asked Jan 13, 2012 at 4:03 PupperPupper 2,3553 gold badges22 silver badges29 bronze badges 2
  • 1 See the second answer (high-voted) stackoverflow./questions/647259/javascript-query-string – Michael Berkowski Commented Jan 13, 2012 at 4:05
  • Cheers Michael, location.search.substring(1) works great. – Pupper Commented Jan 13, 2012 at 4:38
Add a ment  | 

2 Answers 2

Reset to default 6

try this:

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[hash[0]] = hash[1];
        }
        return vars;
}

var url_vars = getUrlVars();
for(var i in url_vars)
{
        alert(i + " == " + url_vars[i]);
}   

mdn

let url = new URL(a.href);
for (const [key, value] of url.searchParams) {

}
发布评论

评论列表(0)

  1. 暂无评论