0

I want to know, how can I get the distance that a div is from window top, by it's id attribute. I've already tried var pubID = "#pub_<?php echo $_GET['pub']; ?>"; alert($(pubID).scrollTop());, and this alert return me "NULL".

I want to use this, when I'am on a determinate page and click on a notification, it redirects the user to another page, and should scroll the new page to the position of the div mentioned on the notification.

Help please, Gonçalo Ribeiro

Gonçalo Ribeiro
  • 115
  • 2
  • 11
  • `$('#yourId').offset().top`. But you cant access the position of the element of the "new page" from the old page. The new page can though find that element and scroll down to it. Or alternatively, just use Anchors to jump down – japrescott Dec 01 '13 at 16:38

1 Answers1

0

Your best bet would be to use jQuery, because offsetTop / offsetLeft don't work consistently in all browsers.

So, assuming you've included jQuery on the page, you could use:

$('#element').offset().top

Also, if you're so inclined, have a read here on the subject here and here.


Edit

Thinking about your problem, would linking to an anchor name (i.e. Adding a hash to the URL) not be easier than trying to work out the position like this?

<a href="newpage.html#anchorName">One link to rule them all</a>
Community
  • 1
  • 1
Labu
  • 2,572
  • 30
  • 34
  • That could indicate a lot of things. I've updated my answer to try and solve your problem and not exactly what you've asked. ;) – Labu Dec 01 '13 at 16:31