How to get URL parameter using jQuery or plain JavaScript?

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

    Perhaps you might want to give Dentist JS a look? (disclaimer: I wrote the code)

    code:

    document.URL == "http://helloworld.com/quotes?id=1337&author=kelvin&message=hello"
    var currentURL = document.URL;
    var params = currentURL.extract();
    console.log(params.id); // 1337
    console.log(params.author) // "kelvin"
    console.log(params.message) // "hello"
    

    with Dentist JS, you can basically call the extract() function on all strings (e.g., document.URL.extract() ) and you get back a HashMap of all parameters found. It's also customizable to deal with delimiters and all.

    Minified version < 1kb

提交回复
热议问题