0

On - window.location.hash - Change?

The above question talks about hash change while this question talks about callback whenever internal link is clicked

I have many links in a page that points to another location in the same page using # links. Whenever, I click one such link, the page scrolls to that location. But I need to adjust the scroll manually after the automatic scroll happens. So would like to know if there is any callback function for such events?

Also the event should fire if the # tag was present in the initial page load (not only when it is clicked with a link) and when the same link is clicked again (hashchange event won't fire in this case)

Community
  • 1
  • 1
Raj
  • 22,346
  • 14
  • 99
  • 142
  • 2
    Keep in mind that, if a user clicks a hash link twice, the hash doesn't change. – Cerbrus Jan 07 '13 at 15:33
  • 1
    @Cerbrus Good point; this isn't exactly the same problem of simply detecting a hash change. – apsillers Jan 07 '13 at 15:36
  • [There's a well known plugin to support the hashchange event](http://benalman.com/projects/jquery-hashchange-plugin/). – zzzzBov Jan 07 '13 at 15:37
  • 1
    To clarify my previous comments: this question is **not** an exact duplicate of a question that asks how to detect a hash change. the user might click on a link for the same hash value, which should trigger some scrolling-correct function but will *not* trigger a hash change. – apsillers Jan 07 '13 at 15:47

2 Answers2

0

You can register one by calling this function on the element:

addEventListener('click', function(){/*update your stuff here/*});

in jQuery, it's even easier

$('a').on('click', function(){/*update your stuff here/*}); will update after every link click.

Eric
  • 3,142
  • 17
  • 14
0

There is no specifica callback that I know of but you could bind a function to the window.onscroll (https://developer.mozilla.org/en-US/docs/DOM/window.onscroll) event. That will detect when the window has been scrolled by the links being clicked and will let you make your adjustments. Then only problem is that it will also fire off when a user scrolls the page normally.

Other than that you would add a class to all your # links that will allow you to detect when one has been clicked. In jQuery:

$("a.HASH-LINK-CLASS").on("click", function (event) { ... });
phonicx
  • 479
  • 2
  • 14