Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    39
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Way to get stuff after url's "#" in javascript?

    Say a user types in the url http://localhost/#dog

    How would you get the string "dog" from the url with javascript? Does the method with all major browsers (ie6+, firefox, safari, chrome)?

    Thanks for the help

  • #2
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    window.location.hash is what you're looking for. It's IE4+, FF1+.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    var url = "http://localhost/#dog";
    var stub = url.replace(/http.+#/,"");
    alert (stub);

    “Going to church does not make you a Christian anymore than going to the garage makes you a car.” - Dr. Laurence J. Peter (American "hierarchiologist", Educator and Writer, 1919-1990)

  • #4
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,696
    Thanks
    0
    Thanked 232 Times in 227 Posts
    ...or maybe this?...
    Code:
    
    var url = "http://localhost/#dog";
    var stub = url.split('/#')[1];
    alert (stub);
    
    coothead

  • #5
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Or maybe you use the builtin function instead of rolling your own.

    You will have to access window.location anyway in order to get to the URL, so why not just use its own hash method.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by venegal View Post
    Or maybe you use the builtin function instead of rolling your own.

    You will have to access window.location anyway in order to get to the URL, so why not just use its own hash method.
    My understanding is that the OP wishes to extract the string dog, not #dog.

  • #7
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    there is a Javascript function so that $_GET[] works in javascript, so you could use $_GET[] after a # e.G #dog=dog, and then you can use the $_GET to get them its how i do it
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)


  •  

    Posting Permissions

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