SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    May 2001
    Location
    Wantgh, NY
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript URL question

    I usually work in ASP and am not very proficient in java script so this may seem like a newbie question.

    I have a page that has a URL parameter value i need to extract, it will look like this

    template.html?img=123

    the name will always be "img"

    in asp i would do it like this

    var_img = request.querystring("img")

    I have found some scripts but they seem to run thru the entire url dividing things into pairs and values, is there a way to just extract the single value i am looking for?

    thanks in advance

  2. #2
    Rehab is for quiters! spartan's Avatar
    Join Date
    Apr 2002
    Location
    Cape Town, South Africa
    Posts
    343
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you trying to do this client-side or server-side?
    Spartan
    ---------------------
    It's like our sergeant told us before one trip into the jungle. Men! Fifty of you are leaving on a mission. Twenty-five of you ain't coming back.
    -Mr.Payne

  3. #3
    SitePoint Member
    Join Date
    May 2001
    Location
    Wantgh, NY
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    client side

  4. #4
    Rehab is for quiters! spartan's Avatar
    Join Date
    Apr 2002
    Location
    Cape Town, South Africa
    Posts
    343
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok
    bit of a mission finding this... I didn't even know it was possible to get the querystring on the client side...

    this'll do it
    Code:
    alert("the querystring is = " +   window.location.search );
    the problem here is that it returns the entire querystring ie "?y=334x=969" so you wont be able to just get straight to the variable you are lokking for. you'll have to chek them all for the one you want.

    so you'll need to remove the '?' from the front, then split the string on the amperstands and the '='s and probably build an array of all the variables and their values and then loop through the array to get the value for the specific one you are looking for!

    thats all I could come up with...


    what a SHLEP! o well, good luck
    Spartan
    ---------------------
    It's like our sergeant told us before one trip into the jungle. Men! Fifty of you are leaving on a mission. Twenty-five of you ain't coming back.
    -Mr.Payne

  5. #5
    Rehab is for quiters! spartan's Avatar
    Join Date
    Apr 2002
    Location
    Cape Town, South Africa
    Posts
    343
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh
    BTW
    a good reference here at DevEdge
    Last edited by spartan; Oct 7, 2002 at 05:12.
    Spartan
    ---------------------
    It's like our sergeant told us before one trip into the jungle. Men! Fifty of you are leaving on a mission. Twenty-five of you ain't coming back.
    -Mr.Payne

  6. #6
    SitePoint Zealot
    Join Date
    Aug 2001
    Location
    DC
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here are some get and set methods for javascript url parameters.

    /*------------------------------------------------------------------
    FUNCTION: This function parses &-separated name=value argument pairs from
    the query string of the URL. It stores the name=value pairs in
    properties of an object and returns that object
    RECEIVES: nothing
    RETURNS: object - array using the parameter name as the index and the value
    as the parameter value
    ------------------------------------------------------------------*/
    function setURLParameters()
    {
    var args = new Object();
    var query = location.search.substring(1); // Get query string.
    var pairs = query.split("&" ); // Break at comma.
    for(var i = 0; i < pairs.length; i++) {
    var pos = pairs[i].indexOf('='); // Look for "name=value".
    if (pos == -1) continue; // If not found, skip.
    var argname = pairs[i].substring(0,pos); // Extract the name.
    var value = pairs[i].substring(pos+1); // Extract the value.
    args[argname] = unescape(value); // Store as a property.
    // In JavaScript 1.5, use decodeURIComponent() instead of escape()
    }
    return args; // Return the object.
    } // END function setURLParameters()


    /*------------------------------------------------------------------
    FUNCTION: Finds the URL parameter given using the array object built by
    getURLParameters();
    RECEIVES: nothing
    RETURNS: object - array using the parameter name as the index and the value
    as the parameter value
    ------------------------------------------------------------------*/
    function getURLParameter(arg)
    {
    // build URL parameters array object
    var URLParameters = setURLParameters();

    // retreive parameter value from URLParameters array
    var argValue = URLParameters[arg];

    //return null if parameter doesn't exist
    if(argValue==null)
    {
    return null;
    } else { //return parameter value
    return argValue;
    } // if(argValue==null)
    } // END function getURLParameter(arg)
    signature

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I made a class for this. It's the "Get Class"
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  8. #8
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by beetle
    I made a class for this. It's the "Get Class"
    Off Topic:

    You have a class for everything! You should teach

  9. #9
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good pun!
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





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
  •