Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: help parsing window.location.href

  1. #1
    Join Date
    Jul 2005
    Posts
    101
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default help parsing window.location.href

    Can anybody tell me how to parse window.location.href to get the page name.
    e.g. thesite/thefolder/thesubfolder/thepage.htm
    and I want to return "thepage"
    I will not know in advance how deep the page name is.
    Thanks

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <script type="text/javascript">
    thePage=unescape(location.href)
    thePage=thePage.substr(thePage.lastIndexOf('/')+1)
    thePage=thePage.substr(0,thePage.indexOf('.')-1)
    alert(thePage)
    </script>
    Not foolproof but, good for normal page names without query strings* containing the '/' character.

    *query strings follow the page name with a '?' and data, ex:

    thesite/thefolder/thesubfolder/thepage.htm?var1=password
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <script type="text/javascript">
    thePage=unescape(location.href)
    thePage=thePage.indexOf('?')==-1? thePage.substr(thePage.lastIndexOf('/')+1) : thePage.substring(thePage.lastIndexOf('/')+1,thePage.indexOf('?'))
    thePage=thePage.substr(0,thePage.lastIndexOf('.')-1)
    alert(thePage)
    </script>
    This one will work even with dots ('.') in the filename and with query strings containing ('/'). There could be other considerations I've missed for other potential location.href values.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Jul 2005
    Posts
    101
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Just the trick. Thanks a lot, John

    Billy

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Actually, looking at it again, my second version will still fail on query strings containing ('/'). Use:
    Code:
    <script type="text/javascript">
    thePage=unescape(location.href)
    if(thePage.indexOf('?')!==-1)
    thePage=thePage.substring(0,thePage.indexOf('?'))
    thePage=thePage.substr(thePage.lastIndexOf('/')+1)
    thePage=thePage.substr(0,thePage.lastIndexOf('.'))
    alert(thePage)
    </script>
    Last edited by jscheuer1; 08-25-2005 at 04:08 PM.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Or just use %2f instead of / in GET query strings, like you're meant to.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    After it is unescaped, what good would that do?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    None... but who mentioned unescaping it?

    /EDIT: Oh wait, you did. I see why; perhaps unescape it after getting the appropriate portion? Check for ., then if it's -1, use %2e instead.
    Last edited by Twey; 08-25-2005 at 04:23 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I think unescaping it in the first place is the safest route, no telling what you are dealing with before that.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You can't escape the path, only the page. Split the page down, then unescape it. You needn't worry about the path.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •