How to get URL parameter using jQuery or plain JavaScript?

前端 未结 30 3957
天涯浪人
天涯浪人 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:55

    This is based on Gazoris's answer, but URL decodes the parameters so they can be used when they contain data other than numbers and letters:

    function urlParam(name){
        var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
        // Need to decode the URL parameters, including putting in a fix for the plus sign
        // https://stackoverflow.com/a/24417399
        return results ? decodeURIComponent(results[1].replace(/\+/g, '%20')) : null;
    }
    

提交回复
热议问题