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

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

    Using URL:

    url = new URL(window.location.href);
    
    if (url.searchParams.get('test')) {
    
    }
    

    EDIT: if you're sad about compatibility, I'd highly suggest https://github.com/medialize/URI.js/.

提交回复
热议问题