AJAX获取URL参数= NULL

时间:2016-06-04 09:39:13

标签: ajax url parameters

我需要重写的网址(htacces)中的网址参数。

我有这个功能,但它返回NULL:

$.urlParam = function(name){
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (results===null){
   return null;
}
else{
   return results[1] || 0;
}
};

这就是我要求参数:

var value = $.urlParam('profile');

我需要从“page.php / profile / 1”获得“1”

提前致谢!

1 个答案:

答案 0 :(得分:0)

在@ weigreen的帮助下,我修改了我的RegExp:

    var results = new RegExp('[/]' + name + '/([^#]*)').exec(window.location.href);

编辑:

对于多个参数(/ profile / 1 / page / 2),这是一个更好的解决方案:

var results = new RegExp('[/]' + name + '/([a-z0-9]+)').exec(window.location.href);