How to check if a query string value is present via JavaScript?

后端 未结 10 1900
眼角桃花
眼角桃花 2021-01-30 00:25

How can I check if the query string contains a q= in it using JavaScript or jQuery?

相关标签:
10条回答
  • 2021-01-30 01:16

    one more variant, but almost the same as Gumbos solution:

    var isDebug = function(){
        return window.location.href.search("[?&]debug=") != -1;
    };
    
    0 讨论(0)
  • 2021-01-30 01:17

    I've used this library before which does a pretty good job of what you're after. Specifically:-

    qs.contains(name)
        Returns true if the querystring has a parameter name, else false.
    
        if (qs2.contains("name1")){ alert(qs2.get("name1"));}
    
    0 讨论(0)
  • 2021-01-30 01:20

    This should help:

    function getQueryParams(){
        try{
            url = window.location.href;
            query_str = url.substr(url.indexOf('?')+1, url.length-1);
            r_params = query_str.split('&');
            params = {}
            for( i in r_params){
                param = r_params[i].split('=');
                params[ param[0] ] = param[1];
            }
            return params;
        }
        catch(e){
           return {};
        }
    }
    
    0 讨论(0)
  • 2021-01-30 01:24

    You could also use a regular expression:

    /[?&]q=/.test(location.search)
    
    0 讨论(0)
提交回复
热议问题