How can I use JavaScript to get an anchor from the URL?
December 16, 2005 1:30 PM   RSS feed for this thread Subscribe

How can I use JavaScript to get an anchor from the URL?

I think this is called the hash in JavaScript, but this doesn't work:

var hash=location.hash;
if (hash){
write a hidden form field with the value of hash
}


For example, if I followed a link to this comment, I'd want to get the value of the comment's anchor, 459821, and write it into a form field.
posted by kirkaracha to computers & internet (4 comments total)
The Firefox DOM Inspector reports that the location object does have a hash attribute. With which browser are you testing?
posted by ijoshua at 1:38 PM on December 16, 2005


It also seems to work for me in Safari. For example, if I navigate to the comment linked above, then type javascript:alert(location.hash) into the address bar and hit enter, I get alerted with the proper value.
posted by ijoshua at 1:41 PM on December 16, 2005


Works for me too (my test page). Perhaps you could put a complete test page online somewhere?
posted by reynaert at 1:54 PM on December 16, 2005


reynaert's code worked on my test page, and it's similar to what I was doing, so I might've had a syntax error.

After thinking it over some more and having some coffee, I realized that I actually want to do something different and don't need to do this, but I appreciate the help.
posted by kirkaracha at 2:20 PM on December 16, 2005


« Older A friend went to Norway and sl...   |   Does anyone have any experienc... Newer »

You are not logged in, either login or create an account to post comments



Related Questions
Help me AJAXify a portion of my project without... January 8, 2008
Looking for web-dev book suggestions February 8, 2007
Mark CF, if you're reading this, shoo! January 23, 2007
What's the step BEFORE "newbie" called? January 4, 2007
Your favorite web design tricks? March 16, 2006