By internal link, for example. to #id is there an event that I can associate with a handler?

I have a web page with various "internal" links to it with various objects with different identifiers. The web page also has a scroll event binding to hide / show certain elements when scrolling by a certain amount.

I would like to call the scroll event handler when the page goes to another section after clicking on one of these links.

The click event is fired before navigation, so I canโ€™t connect to it, because it has to make decisions based on the new location.

+4
source share
2 answers

How about a hashchange event? It is widely supported these days. (Modernizr has a test and many poly regiments, just in case ...)

By the way, if you want to save locations, check out the HTML5 History API . history.pushState should do the trick.

+2
source

The onChange event does not fire when you click on a hash link on one page. The only way modern chronological changes (AJAXed) track hash changes is by using the interval after which the JavaScript function is called to perform this check.

In other words, the only way is to save the last hash location ( document.location.hash ) in a timely manner and return to the previous location if any condition is not met.

0
source

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


All Articles