H_MZ 2016-06-04 09:39 采纳率: 0%
浏览 25

AJAX获取URL参数= NULL

I need URL parameter from a rewritten URL (htacces).

I have this function but it returns NULL:

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

And this is how I'm calling for the parameter:

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

I need to get "1" from "page.php/profile/1"

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • weixin_33716557 2016-06-04 09:53
    关注

    With @weigreen's help I've modified my RegExp:

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

    EDIT:

    For multiple parameters (/profile/1/page/2) this was a better solution:

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

报告相同问题?