I...">

Is it possible to run javascript to run when a specific anchor is selected?

Say I have an anchor in a web page:

<a name="comegetit"></a>

Is there a way to run javascript if the page is received by a link that falls into this anchor? (for example, such a link as <a href="http://myawesomewebpage.com/page#comegetit"></a>). I do not think there is, but I would like it to be.

+3
source share
3 answers

If you expect only one line as a URL anchor:

if(window.location.hash == '#comegetit'){
  // Do your stuff.
}

If you are doing anything else complicated, you can also match window.location.hashagainst a regular expression.

+3
source

I am using Really Simple History ( http://code.google.com/p/reallysimplehistory/ )

- :

window.dhtmlHistory.create();
window.dhtmlHistory.initialize();   
window.dhtmlHistory.addListener(function(hash){
    // this is where you process your hash and do something special
    // and totally funky
});

, - -.

B.S. window.location.hash

+2

You can try to act on the onload event and check that the contents of window.location retrieve the fragment identifier ... it should work.

+1
source

Source: https://habr.com/ru/post/1738911/


All Articles