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

后端 未结 10 1906
眼角桃花
眼角桃花 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:06

    Try this

    //field "search";
    var pattern = /[?&]search=/;
    var URL = location.search;
    
    if(pattern.test(URL))
    {
        alert("Found :)");
    }else{
        alert("Not found!");
    }
    

    JSFiddle: https://jsfiddle.net/codemirror/zj4qyao2/

提交回复
热议问题