0

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

Can the hashbang in the url, #boom be linked to triggering a function like boom() on the page?

Thanks!

fancy
  • 48,619
  • 62
  • 153
  • 231

4 Answers4

4

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.

Digital Plane
  • 37,354
  • 7
  • 57
  • 59
1

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.

ChrisJ
  • 5,161
  • 25
  • 20
0

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

ShaneC
  • 2,376
  • 4
  • 19
  • 27
0

Maybe something like this:

window.onload = function(){ // Page onload
    if(location.hash == "#boom"){ // Hash is "boom"
        boom(); // call function boom()
    }
}
Floern
  • 33,559
  • 24
  • 104
  • 119