How to get URL parameter using jQuery or plain JavaScript?

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

    http://example.com?sent=yes
    

    Best solution here.

    function getUrlParameter(name) {
        name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
        var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
        var results = regex.exec(location.href);
        return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, '    '));
    };
    

    With the function above, you can get individual parameter values:

    getUrlParameter('sent');
    

提交回复
热议问题