How can I use JavaScript to get an anchor from the URL?
December 16, 2005 1:30 PM   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 answers 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

Response by poster: 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 Where can I find a split comforter?   |   Do hormones mess with your head? Newer »
This thread is closed to new comments.