开发者

Use anchors/hashbangs in the URL to trigger functions on load?

Say someone is linked to the url, example.com/#boom

Can the hashbang in the u开发者_JAVA百科rl, #boom be linked to triggering a function like boom() on the page?

Thanks!


I think you want to do something like this:

window.onload = function(){
  var hash = location.hash.substr(1);
  if(typeof window[hash] == "function")
    window[hash]();
};

If the function specified in the hash exists, then it will be called on page load.


Not sure to understand what you really want... On your web page, you can add code that runs at page load, that examines the URL and that calls a suitable function accordingly. You will want to use window.location for this.


Easily done with JavaScript:

if( window.location.hash ){
    var currentHash = window.location.hash;
    alert( "Yup! " + currentHash + " is set." );
}else{
    alert( "Nope!" );   
}

Now this is an example. Obviously you would want to call your call back function instead of outputting currentHash.

For more information on calling the function: http://w3future.com/html/stories/callbacks.xml


Maybe something like this:

window.onload = function(){ // Page onload
    if(location.hash == "#boom"){ // Hash is "boom"
        boom(); // call function boom()
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜