1

I was wanting to make a link similar to this

www.mysite.com/profile/#openTheme

When the link has the hash "#openTheme" i want it to run a function on the page when the page is loaded.

Is there a way to set an Anchor like this eg:

<a name="openTheme" onActivate="runScript()">

Or is there a better way of doing it? Eg, running a script onload to find any location hashes and using if/else?

Thanks.

1
  • It wouldn't be that simple, are you willing to use a framework like jQuery or should I hack something together in regular JavaScript? Commented Nov 18, 2010 at 1:46

2 Answers 2

2

You can pick this up using this:

self.document.location.hash

This will return #openTheme in this case. You can then do an if, so...

if(self.document.location.hash == '#openTheme')
{
    //do something
}
Sign up to request clarification or add additional context in comments.

1 Comment

Worked a treat! Thanks chief.
0

You can register the window.onhashchange event, which will fire whenever the user types a new hash into the address bar, or a link is clicked that points to a hash on the current page, or JavaScript sets location.href to "#something-or-other".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.