来自URL Javascript的参数 - 表格:/index.html#/?id = 1426591453147

时间:2015-03-17 15:07:25

标签: javascript jquery url web parameters

我试图使用JavaScript或jQuery从当前网址获取参数。 URL如下所示:

http://my-site.com/index.html#/?id=1426591453147

http://my-site.com/#/?id=1426591453147

我用“location.search”尝试了一些代码,但location.search在我的网址上返回一个空字符串。

有没有人知道这个问题的好方法?

修改 我最终使用了这个:

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

1 个答案:

答案 0 :(得分:0)

这样的东西? (适用于您的具体案例

"http://my-site.com/index.html#/?id=1426591453147".replace(/^.*?\.com(.*?)$/i, '$1'); // "/index.html#/?id=1426591453147"

"http://my-site.com/#/?id=1426591453147".replace(/^.*?\.com(.*?)$/i, '$1'); // "/#/?id=1426591453147"

所以,

location.href.replace(/^.*?\.com(.*?)$/i, '$1');