How to get URL parameter using jQuery or plain JavaScript?

前端 未结 30 3202
天涯浪人
天涯浪人 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 07:00

    This will give you a nice object to work with

        function queryParameters () {
            var result = {};
    
            var params = window.location.search.split(/\?|\&/);
    
            params.forEach( function(it) {
                if (it) {
                    var param = it.split("=");
                    result[param[0]] = param[1];
                }
            });
    
            return result;
        }
    

    And then;

        if (queryParameters().sent === 'yes') { .....
    

提交回复
热议问题