从页面网址中获取名称/值对

时间:2014-06-09 17:47:53

标签: javascript html url

我在这里有一个页面:

http://recipehappy.com/share.php?user=Mains&PID=2&passkey=bmAjWDMwmWcOBLoU4B0q2NARCG0PBTc1

我需要将PID值和passkey值放到页面上的JavaScript变量中。这可能很简单,但我被阻止了。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

这就是我使用的

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

ex:getParameterByName('PID')

来源:https://stackoverflow.com/a/901144/643761