What JavaScript library should I use for parsing URL parameters?

前端 未结 8 1545
终归单人心
终归单人心 2020-12-30 09:52

How do I parse URL parameters in JavaScript? (These are the parameters I would ordinarily call GET parameters or CGI parameters, but in this case the page is basically subm

相关标签:
8条回答
  • 2020-12-30 10:16

    Javascript has no built in support for URL parameters.

    Anyway, the location.search property returns the portion of current page URL starting from the question mark ('?').

    From this, you can write your own parameter parser or you can make use of one of those available in most common Javascript frameworks, such as JQuery and similar.

    0 讨论(0)
  • 2020-12-30 10:18

    I use the parseUri library available here: http://stevenlevithan.com/demo/parseuri/js/

    It allows you to do exactly what you are asking for:

    var uri = 'http://google.com/?q=stackoverflow';
    var q = uri.queryKey['q'];
    // q = 'stackoverflow'
    

    I've been using it for a while so far and haven't had any problems.

    0 讨论(0)
  • 2020-12-30 10:20

    Here's are two simple functions that do the job : http://adamv.com/dev/javascript/querystring

    Here is a sample of the API Reference :

    var qs = new Querystring();
    
    // Parse a given querystring
    var qs2 = new Querystring("name1=value1&name2=value2");
    
    var v1 = qs2.get("name1");
    var v3 = qs2.get("name3", "default value");
    
    0 讨论(0)
  • 2020-12-30 10:31

    If it's "submitting to itself," do you need to do GET parameters?

    But if you do, most browsers now have the decodeURIComponent function which will handle individual parameters; your job is to split them on & (String#split will do that nicely). If you want a library, jQuery and Prototype are both well-used and tested.

    0 讨论(0)
  • 2020-12-30 10:34

    I found this useful for simple url parsing, modifying url (like adding new query params): https://github.com/derek-watson/jsUri

    0 讨论(0)
  • 2020-12-30 10:35

    jQuery URL Utils or jQuery URL Parser.

    0 讨论(0)
提交回复
热议问题