首页 > 解决方案 > 字符串而不是 window.location.search

问题描述

如何从 javascript 中的字符串获取 url 参数?而不是使用 window.location.search。例如,让 ="https://google.com/hello?name=roger&age=20" 我需要获取我在此示例中找到的所有参数,但它使用 window.location.search

var urlParams;
(window.onpopstate = function () {
    var match,
        pl     = /\+/g,  // Regex for replacing addition symbol with a space
        search = /([^&=]+)=?([^&]*)/g,
        decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
        query  = window.location.search.substring(1);

    urlParams = {};
    while (match = search.exec(query))
       urlParams[decode(match[1])] = decode(match[2]);
})();

标签: javascript

解决方案


推荐阅读