Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if a page has jumped to the anchor (#) in javascript?

I have some javascript that can appear on many different pages. Sometimes those pages have been accessed via a URL containing an anchor reference (#comment-100, for instance). In those cases I want the javascript to delay executing until after the window has jumped. Right now I'm just using a delay but that's pretty hackish and obviously doesn't work in all cases. I can't seem to find any sort of DOM event that corresponds to the window "jump".

Aside from the simple delay, the only solution I've come up with is to have the JS look for the anchor in the URL and, if it finds one, watch for changes in scrollTop. But that seems buggy, and I'm not 100% sure that my script will always get fired before the scrolling happens so then it would only run if the user manually scrolled the page. Anyhow, I don't really like the solution and would prefer something more event driven. Any suggestions?

Edit to clarify:

I'm not trying to detect a hash change. Take the following example:

  1. Page index.php contains a link to post.php#comment-1
  2. User clicks the link to post.php#comment-1
  3. post.php#comment-1 loads
  4. $(document).ready fires
  5. Not long later the browser scrolls down to #comment-1

I'm trying to reliably detect when step 5 happens.

like image 575
Aaron Avatar asked Aug 24 '10 21:08

Aaron


People also ask

What is an anchor link example?

Creating Anchor Links For example, it could be a 'back to top' link that takes the user to the top of the current page, or it could be a link that takes a user directly to a particular section located the middle of another page entirely. For a normal link, the thing you want to link to has a URL of its own.

How do I scroll to an anchor?

The easiest way to to make the browser to scroll the page to a given anchor is to add *{scroll-behavior: smooth;} in your style. css file and in your HTML navigation use #NameOfTheSection . This CSS method works great for me and is super elegant!

How do I link to an anchor on the same page?

To link to your newly-created named anchor, highlight the text, then click the Linkit button. In the "Link URL" field, add a "#" symbol and your anchor name: Click "Insert link" to add the link to your named anchor (on the same page).


2 Answers

You can check window.onhashchange in modern browsers. If you want cross compatible, check out http://benalman.com/projects/jquery-hashchange-plugin/

This page has more info on window.onhashchange as well.

EDIT: You basically replace all anchor names with a similar linking convention, and then use .scrollTo to handle the scrolling:

$(document).ready(function () {
  // replace # with #_ in all links containing #
  $('a[href*=#]').each(function () {
      $(this).attr('href', $(this).attr('href').replace('#', '#_'));
  });

  // scrollTo if #_ found
  hashname = window.location.hash.replace('#_', '');
  // find element to scroll to (<a name=""> or anything with particular id)
  elem = $('a[name="' + hashname + '"],#' + hashname);

  if(elem) {
       $(document).scrollTo(elem, 800,{onAfter:function(){
        //put after scroll code here }});
  }
});

See jQuery: Scroll to anchor when calling URL, replace browsers behaviour for more info.

like image 105
bryan.taylor Avatar answered Sep 20 '22 00:09

bryan.taylor


Seems like you could use window.onscroll. I tested this code just now:

<a name="end" />
<script type="text/javascript">
    window.onscroll = function (e) {
        alert("scrolled");
}
</script>

which seems to work.

Edit: Hm, it doesn't work in IE8. It works in both Firefox and Chrome though.

Edit: jQuery has a .scroll() handler, but it fires before scrolling on IE and doesn't seem to work for Chrome or Firefox.

like image 26
Gilsham Avatar answered Sep 19 '22 00:09

Gilsham