简体   繁体   English

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

[英]How to get query string parameter from SEO-friendly URL?

I switched from using normal URL parameters to SEO-friendly URLs. 我从使用普通的URL参数切换为SEO友好的URL。 For example, I have gone from this... 例如,我已经离开了...

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

... to this: ...对此:

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

Using JavaScript or jQuery, what would be the best way to get the id variable of 1? 使用JavaScript或jQuery,获取id变量1的最佳方法是什么?

I currently use a function like this: 我目前使用这样的功能:

// 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];
    }
}

Is there a better way or am I stuck splitting the URL via the /? 有没有更好的方法,还是我想通过/?分割URL /? ? And how would you handle multiple parameters? 您将如何处理多个参数?

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

最简单的可能是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM