如何从 SEO 友好的 URL 获取查询字符串参数



我从使用普通URL参数切换到SEO友好的URL。例如,我已经从这里走了...

http://www.example.net/mypage.aspx?id=1

。对此:

http://www.example.net/mypage/1

使用 JavaScript 或 jQuery,获取 id 变量 1 的最佳方法是什么?

我目前使用这样的函数:

// get the values from the query string.
function GetQueryStringParams(sParam) {
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++) {
    var sParameterName = sURLVariables[i].split('=');
    if (sParameterName[0] == sParam) {
        return sParameterName[1];
    }
}

有没有更好的方法,或者我是否通过/?拆分 URL ?您将如何处理多个参数?

http://www.example.net/mypage/1/2/3/4/5

最简单的可能是

var sPageURL = window.location.href.split('/').pop();

最新更新