Welcome to WebmasterWorld Guest from 54.160.254.203

Forum Moderators: open

Message Too Old, No Replies

Calling Javascript function from Anchor Tags

Eg: http://www.example.com/index.html#funtion1 would call function1()

     
6:09 pm on Sep 29, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 3, 2007
posts:131
votes: 0


Greetings,
I tried searching the forum for an answer on this topic, but I did not find a specific answer. Therefore I decided to start this thread. Hope my question is simple (seems like it is): Is there any possible way to call a function on Javascript from simply changing the anchor (# at the end of the URL address)?

I see websites that use Tabs and automatically change to a different tab depending on the anchor tag specified. I want to know if you guys have any ideas how this might be possible using Javascript.

Thanks,

7:41 pm on Sept 29, 2008 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts:4966
votes: 10


You could get the window.location.hash property and perform some action based on the value.


var h = window.location.hash;
if (h == "#foo") {
// do something
}
else if (h == "#bar") {
// do something else
}
8:04 pm on Sept 29, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 3, 2007
posts:131
votes: 0


Fotiman. Thank you for your quick answer!

The solution is pretty simple, just how I'd expect it.