开发者

Using Hashes in URL to Run Script onload

开发者 https://www.devze.com 2023-01-25 09:49 出处:网络
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.

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 i开发者_Python百科s there a better way of doing it? Eg, running a script onload to find any location hashes and using if/else?

Thanks.


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
}


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".

0

精彩评论

暂无评论...
验证码 换一张
取 消