How to get URL parameter using jQuery or plain JavaScript?

前端 未结 30 3253
天涯浪人
天涯浪人 2020-11-21 06:29

I have seen lots of jQuery examples where parameter size and name are unknown.

My URL is only going to ever have 1 string:

http://example.com?sent=ye         


        
30条回答
  •  北荒
    北荒 (楼主)
    2020-11-21 06:57

    This one is simple and worked for me

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

    so if your url is http://www.yoursite.com?city=4

    try this

    console.log($.urlParam('city'));
    

提交回复
热议问题