I saw some of websites executes a JavaScript function based on has in the URL. For example,
when I access http://domain.com/jobs#test
then the website executes a function based on #test
I can do it by checking location.href, but is there a better way?
This is what i do:
demo: http://jsfiddle.net/maniator/XCjpy/show/#test
demo2: http://jsfiddle.net/maniator/XCjpy/show/
demo3: http://jsfiddle.net/maniator/XCjpy/show/#testing_again
Live Demo
Note:
eval()
is dangerous. You should make a predefined array of safe functions, and call those.Have a look at This.
See the property table.
location.hash should help you.
You could use a library like YUI to do this more elegantly, Take a look at the YUI Browser History Manager
http://developer.yahoo.com/yui/history/
If you don't need to support old browsers like IE6 and IE7 you can use:
But if you have to support older browsers you need to poll: