简体   繁体   中英

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

I switched from using normal URL parameters to SEO-friendly URLs. 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?

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 /? ? And how would you handle multiple parameters?

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

最简单的可能是

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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