Safari and offsetTop/Left issues

Posted on 2006-05-15
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
    LVL 14

    Expert Comment

    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

    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

    I looked at it logically and came to this result:

    The guy at 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

    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        !!!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Javascript: Crash Course

    Javascript is not only emerging as the most important language to know, it's flexibility is unique. With Javascript being deployed on the web, mobile and even the desktop this is the one language that all technologists need to know.

    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
    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…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now