SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to get url parameter values with JavaScript

    How do you extract the parameters from a URL with JavaScript. For example I have a list of urls such as http://domain/musicapp/player.html?id=1 and http://domain/musicapp/player.html?id=2. How can I ensure that when I click one of the links that I pull the value of the id using JavaScript.

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,597
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Everything in the URL starting with the ? is contained in the location.search variable. To extract the values from there you need to first split on any & characters so as to get each name/value pair separately and then split each on the = to separate the name and the value.


    For example the following function will return an object where each field named in the URL is returned as a property that has the value specified.:

    Code:
    function qs() {"use strict";
    var query, parms, i, pos, key, val, qsp;
    qsp = {};
    query = location.search.substring(1);
    parms = query.split('&');
    for (i=parms.length-1; i>=0; i--) {
       pos = parms[i].indexOf('=');
       if (pos > 0) {
          key = parms[i].substring(0,pos);
          val = parms[i].substring(pos+1);
          qsp[key] = val;
          }
       }
    return qsp;
    }
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So the value of id will be stored in what variable then qsp [0].val???

  4. #4
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Code:
    parms = query.split('&');
    I would recommend:
    Code:
    parms = query.split( /&(amp;)?/i  );
    Tab-indentation is a crime against humanity.

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Logic Ali View Post
    I would recommend:
    Code:
    parms = query.split( /&(amp;)?/i  );
    How do you retrieve tbe value of id though?

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,597
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    With that function you can use

    Code:
    var parms = qs();
    and then the value of the id will be in parms.id
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •