[Last Call] Learn how to a build a cloud-first strategyRegister Now


Safari and offsetTop/Left issues

Posted on 2006-05-15
Medium Priority
Last Modified: 2012-06-27
I have a piece of code here which returns the current top and left position of the mouse pointer:

var currentItem = menuItem;
            var top = 0;
            var left = 0;
            while(currentItem != null)
                  top += currentItem.offsetTop;
                  if(menuLevel != 0){
                        left += currentItem.offsetLeft - 3;
                        left += currentItem.offsetLeft;
                  currentItem = currentItem.offsetParent;
            left += menuItem.offsetWidth;
The final result returns top and left values (x and y coordinates). It works for everything else but Safari  - which seems to return 0 for all values.  I searched the web site but can't find any answer for that.  Can someone tell me how to get around it.  Thanks.
Question by:waiman
  • 3
LVL 14

Expert Comment

ID: 16688662
Please have a look at


Javascript --> DHTML --> Find position

Compare your script to the scripts listed there (fairly at the bottom)
Also, as you'll see on that page, there is no bug referenced to Safari for the issue of
finding position with offsetLeft/offsetTop.

I am 'only' a javascript newbie ;)) but I think you have to reference your .offsetParent line
before you try to locate position using .offsetLeft and .offsetTop

Hope it helps.

Author Comment

ID: 16696581
I actually had a look at it but it does not seem to solve my problem,..... LEFT always return 0 for some reasons......
LVL 14

Expert Comment

ID: 16697190
I looked at it logically and came to this result:

The guy at quirksmode.org has lots of experience with browser issues
in relation to javaScript, it's his job so to say, so if he's not objecting
(and he isn't) to use offsetTop and offsetLeft with Safari, then those
two attributes are definitely not the problem in your code.

I look at this from your code:  

   left += menuItem.offsetWidth;

and don't understand why you put this there.
You actually like to get offsetLeft (cursor position) to your left variable
but then you add menuItem.offsetWidth to it, which I think is the problem
why you end up getting 0 for your Safari LEFT value.

LVL 14

Accepted Solution

xberry earned 2000 total points
ID: 16697232
Just found this one:


You'll note on top right that it says: "The example script doesn't work in Safari".
Then down the paragraphs you'll find the script and it contains . . .
      . . . offsetWidth        !!!

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question