views:

64

answers:

4

Is it possible to use HTML and/or Javascript to automatically work out what the next page anchor is and go to it?

Thanks

+1  A: 

Assuming you mean relative to a known anchor/item; if you are using jQuery, they have a selector for "Next Sibling"; find the documentation here:

http://api.jquery.com/next-siblings-selector/

I am just running out; otherwise I would build up the full selector for you, but if you don't have an answer later today, I will put it together.

Edit

Actually, based on further reading, I am quessing you are looking for the next anchor relative to the current scroll position on the page... that is a little more work... but is doable... (again, I will try to get on later today to provide the JS).

Edit

These are not 100% complete; but should be enough so you get the general idea. If your page is static content, I would recommend going through the page and caching all offsets on document ready; determining an elements offset can be pretty slow (especially in older browsers).

var _anchorOffets = {};

$(document).ready(function () {
  $("A[id]:not(A[href])").each(function (index, value) {
    var $anchor = $(value);
    _anchorOffets[$anchor.attr("id")] = $anchor.offset().top;
  });
});

function getNextAnchorId() {
  var $window = $(window);
  var minOffset = $window.scrollTop() + $window.height();

  for (var anchor in _anchorOffets) {
    if (_anchorOffets[anchor] >= minOffset) {
      return anchor;
    }
  }

  return null;
}

alternatively, if you page has dynamic content and the anchor locations are not fixed within the document, then you would be looking at something closer to

function getNextAnchorId() {
  var result = null;

  $("A[id]:not(A[href])").each(function (index, value) {
    var $anchor = $(value);
    var $window = $(window);
    var minOffset = $window.scrollTop() + $window.height();

    if($anchor.offset().top >= minOffset) {
      result = $anchor.attr("id");
      return false;
    }
  });

  return result;
}

Depending on your page, you may need to expand on this idea. Both of the above solutions assume a pretty basic document layout. If you have anything fancy going on with relative/absolute positing, or scrollable divs etc, you will need to beef this up (I can provide code if that is the case). For now though I am assuming that we are talking about a basic find the anchors on the page, and figure out which anchor would be next based on scroll position. Also, I didn't extensively test, so some polish may also be required :D

Calgary Coder
Sorry I didn't clarify in the question, I meant relative to the current page position. Is there anything in JQuery that lets you get the current anchor from the page position? (I can't find anything, but I could have missed something.)Edit: Ok, thanks. I may not be able to respond til tomorrow (I'm UK so it's coming up to evening here).
hrickards
A: 

I think I understand what you're looking for. If you know the id's you can use this...

function Next(){
    var lnkNext = document.getElementById("yourNextAnchorId");
    window.location.href = lnkNext.src;
}

if you don't know the id's maybe get the anchors by the innerText

Hooger
Thanks for the answer, but I managed to find another way to do it.
hrickards
+1  A: 

Reading your comment to Calgary Coder, I believe - if I understand you correctly - you're looking to find out the anchor in the current URL. You can do this with window.locaiton.hash. E.g.

alert(window.location.hash);
Gert G
Thanks. The only problem with that is that if the user scrolls down, they won't have an anchor in their URL. Is their any similar code that can find out the anchor from the current scroll position?
hrickards
Unfortunately not, unless they click another anchor on your page or if you somehow force anchor jumps when they pass certain points on the page (I wouldn't recommend this approach even if it happens to be doable... it might become a rather jerky experience for the user).
Gert G
But you might be able to check the scroll position, in a similar way to what this guy does: http://articles.sitepoint.com/article/preserve-page-scroll-position
Gert G
A: 

Thanks for the answers everyone, but I managed to get this working by implementing http://marcgrabanski.com/articles/scrollto-next-article-button-jquery. This works by going to the next header, rather than the next anchor but I imagine if it's critical to use anchors it could be easily adapted to do so.

hrickards
Interesting... there are so many jQuery plugins out there :D Looks like a fair bit of code, but as long as it does the trick, good enough. Nice find.
Calgary Coder