SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Guru DenverDave's Avatar
    Join Date
    Feb 2001
    Location
    Denver, Colorado
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Parsing url parameters

    I have a url with parameters like the following:

    http://test.com/mycode.html?xyz&myvar=value2

    I need to endup with the following available to JS

    base_file = mycode.html
    parameter1 = xyz
    parameter2 = value2 or myvar = value 2

    What is the easiest way to parse the query string out of the url? When I use self.location I get everything.

    Thanks.

  2. #2
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would do something like

    Code:
    document.location.search
    and split each value, unless your searchig for a specific, in which you might as well use indexOf to get the value that your after.

  3. #3
    SitePoint Guru DenverDave's Avatar
    Join Date
    Feb 2001
    Location
    Denver, Colorado
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So use indexOf to find the ? that starts the first parameter and the & the second. ... I think I can handle that - I've seen examples. - Thanks.

    Any idea on the best way to determine the file name without all of the http://test.com and query paramenters. I just want the mycode.html filename.

    Thanks for you help.

  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Try document.url;

    --Vinnie

  5. #5
    SitePoint Guru DenverDave's Avatar
    Join Date
    Feb 2001
    Location
    Denver, Colorado
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    document.url gives the full path not just the filename, everything else seems to be available as a property.
    Following is a description of each part of the URL diagram:

    protocol represents the beginning of the URL, up to and including the first colon.
    host represents the host and domain name, or IP address, of a network host.
    port represents the communications port that the server uses for communications.
    pathname represents the url-path portion of the URL.
    hash represents an anchor name fragment in the URL, including the hash mark (#). This property applies to http URLs only.
    search represents any query information in the URL, including the question mark (?). This property applies to http URLs only. The search string contains variable and value pairs; each pair is separated by an ampersand (&).
    Last edited by DenverDave; Dec 4, 2002 at 16:58.

  6. #6
    SitePoint Guru DenverDave's Avatar
    Join Date
    Feb 2001
    Location
    Denver, Colorado
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is strange... in my example:

    http://myDomain.com/test.html?mode=yes

    location.pathname
    yields:

    location.pathname= /test.html

    Silly, me I thought the pathname property would be the pathname rather than the part of the URL that is not the path.... but works for me...

    window.location.pathname.substring(1,window.location.pathname.length) to get the actual file name being used

    Anyone have a better way?

  7. #7
    SitePoint Enthusiast hooha's Avatar
    Join Date
    Apr 2002
    Location
    Arizona, USA
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A regular expression would be ideal for this purpose, though they are kinda tricky if you've never used them before.

    Try a Google search for 'regular expression url' or something like that. I've found several for this purpose, but none which will parse all of the parameters into variables (no doubt it's out there, though.)

    Take a look at string.match() or RexExp.exec()
    *| Now with 98% less stank... |*

  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)
    Originally posted by hooha
    A regular expression would be ideal for this purpose, though they are kinda tricky if you've never used them before.

    Try a Google search for 'regular expression url' or something like that. I've found several for this purpose, but none which will parse all of the parameters into variables (no doubt it's out there, though.)

    Take a look at string.match() or RexExp.exec()
    Regular expression headquarters. Free regex's to use to your heart's content.

    --Vinnie

  9. #9
    SitePoint Enthusiast hooha's Avatar
    Join Date
    Apr 2002
    Location
    Arizona, USA
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That the site I use most often for regExs, too! When I searched for one to parse URLs, the only one I could find which will capture various parts (scheme, host, etc...) didn't work when I tested it.

    This one will validate a URL:
    ^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$
    but you'd need to add repeatable subexpression to parse out the URL parameters. Maybe something like...
    \?(.*[^\&])+
    (find the question mark, grab everything up to an ampersand and repeat. I don't think this will work as-is, but it's a beginning. (Repetition is a pain... )

    Anyway, once you work out the expression, string.match() and some other regexp methods will return an array with all the captured expressions (The scheme and host and stuff first, then params like 'paramOne=1' and 'paramTwo=2').
    *| Now with 98% less stank... |*


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
  •