URL Javascript-Form:/index.html#/?id=1426591453147中的参数
我正在尝试使用JavaScript或jQuery从当前url获取参数。 URL如下所示:URL Javascript-Form:/index.html#/?id=1426591453147中的参数,javascript,jquery,url,web,parameters,Javascript,Jquery,Url,Web,Parameters,我正在尝试使用JavaScript或jQuery从当前url获取参数。 URL如下所示: http://my-site.com/index.html#/?id=1426591453147 或 我尝试了一些“location.search”代码,但location.search在我的URL上返回一个空字符串 有人知道解决这个问题的好办法吗 编辑: 我最后用了这个: function getParameterByName(name) { name = name.replace(/[\[]/
http://my-site.com/index.html#/?id=1426591453147
或
我尝试了一些“location.search”代码,但location.search在我的URL上返回一个空字符串
有人知道解决这个问题的好办法吗
编辑:
我最后用了这个:
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, " "));
}
像这样的?(适用于您的具体案例)
所以
location.search
在URL的格式为:example.com?foo=bar
时起作用。但是您的“查询字符串”前面是
,因此,字符串的其余部分是通过location.hash来访问的http://my-site.com/index.html#/?id=1426591453147“.replace(/^.*?\.com(.*?$/i,'$1')
我最终使用了zvonas的答案。谢谢
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, " "));
}
"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');