473,698 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getting incorrect offsetleft and offsettop values in IE

85 New Member
have a problem with this code when working in IE
it gives me 0,0 coords when i use iframe tag but gives correct coords when using firefox

Expand|Select|Wrap|Line Numbers
  1. function GetRealOffset(id)
  2. {
  3.  
  4.     var elem = document.getElementById(id);
  5.     var leftOffset = elem.offsetLeft;
  6.     var topOffset = elem.offsetTop;
  7.     var parent = elem.offsetParent;
  8.  
  9.         while(parent != document.body) 
  10.     {
  11.              leftOffset += parent.offsetLeft;
  12.          topOffset += parent.offsetTop;
  13.             parent = parent.offsetParent;
  14.     }
  15.         var Offsets = new Object();
  16.     Offsets.top = topOffset;
  17.     Offsets.left = leftOffset;
  18.  
  19.         alert(Offsets.top + " " +Offsets.left)
  20.     return Offsets;
  21. }
i found out some more function and they r all giving me 0,0 coords
i read in a tutorial that IE gives 0,0 coords for all elements unless the page is loaded but i want the offset values when the page is loading coz im putting advertisements in the iframes and some pages can take minutes to load.
All these functions r working fine in mozilla tho
i think what i need it a function to get the correct offset values when the page is loading in IE
Jul 27 '07 #1
2 11096
pbmods
5,821 Recognized Expert Expert
Heya, rohitchawla. Welcome to TSDN!

Check out this article.
Jul 27 '07 #2
kovik
1,044 Recognized Expert Top Contributor
Don't check against document.body, check against whether or not the offsetParent is null or not. The highest element has a null (non-existant) offsetParent.
Jul 28 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2382
by: Pieter Van Waeyenberge | last post by:
Hello In the code below, theres a DIV, a TABLE and another DIV , each with an image in it. When i read out the offsetLeft/Top from imgB in the table , i get offset values relative to the containng TD ... *as it should be*, HOWEVER, the offsetLeftTop values for imgA/C in the DIVs are calulated realtive to the window border ???? is this normal
1
2032
by: Weston C | last post by:
Here's something I'm working on: for a click on a given element, I want to be able capture the x/y coordinates of the mouse -- that is, the mouse coordinates relative to the top left corner of said element. So far, here's what I've got: http://weston.canncentral.org/misc/webgallery/Haven/pie.html (Code for the function given in this post below).
21
3972
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does nothing in IE. I'd be greatful for any assistance. Also, if I will have problems the code on Opera or Safari, I'd appreciate any pointers--I don't have a Mac to test Safari. THanks very much, Michael
6
16531
by: Seth Illgard | last post by:
Hi. I need to obtain the exact postion of a div. I was using (variable and function names are in spanish, sorry): //Get X position function obtenerPosicionX(elemento) { var x=0; while(elemento) { x += elemento.offsetLeft;
1
3655
by: Orion | last post by:
In my code i am trying to access offsetLeft and offsetTop values for a element which is a div, I use offsetParent property to obtain the same. The code i use is this.tip.style.left = this.handle.offsetParent.offsetLeft + 'px'; After debugging i found the value of "this.handle.offsetParent" is set to null. I have problems with Mozilla firefox. The code works absolutly fine with IE My questions :-
5
5472
by: montybytes | last post by:
Hi there, Although, I have already placed this question in the HTML/CSS section, perhaps it might be worthwhile asking the question here as well. I have a JavaScript function which retrieves the left-position of an Element. This JavaScript functions works in FF and IE7 but does not work in IE6, In IE6 it retrieves the wrong offsetLeft value. Having investigated the problem I noticed it had something to do with the margin-left and position...
3
4908
by: Nitinkcv | last post by:
Hi all, Im trying to do the following: Have 3 divs which are placed side by side. each has a two links 'left' and 'right'. Say on click of the 'left' hyperlink of the 2nd div(middle), the 1st div should take the place of the middle div, while the middle one should take the place of the 1st place. I'm using the prop offsetLeft, however have run into some issues. My code is shown below:
4
2374
by: N00b13 | last post by:
I have a great JS menu but I have to update every page each time I want to change a link. Is there a way to store my links in a file and call it so i only change that file? (what I have tried so far has not worked, but then again my JS really never works :) ) my .js: if (typeof Scl == "undefined") { var Scl = {}; } (function()
0
8675
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8862
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.