How to get URL parameter using jQuery or plain JavaScript?

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

    What if there is & in URL parameter like filename="p&g.html"&uid=66

    In this case the 1st function will not work properly. So I modified the code

    function getUrlParameter(sParam) {
        var sURLVariables = window.location.search.substring(1).split('&'), sParameterName, i;
    
        for (i = 0; i < sURLVariables.length; i++) {
            sParameterName = sURLVariables[i].split('=');
    
            if (sParameterName[0] === sParam) {
                return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
            }
        }
    }
    

提交回复
热议问题