How to get URL parameter using jQuery or plain JavaScript?

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

    There's this great library: https://github.com/allmarkedup/purl

    which allows you to do simply

    url = 'http://example.com?sent=yes';
    sent = $.url(url).param('sent');
    if (typeof sent != 'undefined') { // sent exists
       if (sent == 'yes') { // sent is equal to yes
         // ...
       }
    }
    

    The example is assuming you're using jQuery. You could also use it just as plain javascript, the syntax would then be a little different.

提交回复
热议问题