SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    offesetLeft and offsetTop gives wrong coords in IE

    i 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

    Code:
    function GetRealOffset(id)
    {
    	
    	var elem = document.getElementById(id);
    	var leftOffset = elem.offsetLeft;
    	var topOffset = elem.offsetTop;
    	var parent = elem.offsetParent;
    	
            while(parent != document.body) 
    	{
                 leftOffset += parent.offsetLeft;
    	     topOffset += parent.offsetTop;
                parent = parent.offsetParent;
    	}
            var Offsets = new Object();
    	Offsets.top = topOffset;
    	Offsets.left = leftOffset;
    	
            alert(Offsets.top + " " +Offsets.left)
    	return Offsets;
    }
    i use the iframe tag to display advertisements in them and need the coords of iframe tag for IE
    im new to javascript and dont know much bout this
    plz help me with this
    thx in advance

  2. #2
    I'm not a human
    Join Date
    Aug 2006
    Location
    India
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi

    these are the two function i mostly use to find the top and left of a given object.

    and it's working nicely both in IE and firefox..


    try these functions.

    Code:
    function findPosX(obj)
    {
    	var curleft = 0;
    	if (obj.offsetParent)
    	{
    		while (obj.offsetParent)
    		{
    			curleft += obj.offsetLeft
    			obj = obj.offsetParent;
    		}
    	}
    	else if (obj.x)
    		curleft += obj.x;
    	return curleft;
    }
    
    function findPosY(obj)
    {
    	var curtop = 0;
    	if (obj.offsetParent)
    	{
    		while (obj.offsetParent)
    		{
    			curtop += obj.offsetTop
    			obj = obj.offsetParent;
    		}
    	}
    	else if (obj.y)
    		curtop += obj.y;
    	return curtop;
    }
    
    i would call the function as 
    
    var x =document.getElementById(aa);
    var tt =findPosX(x);
    Regards,
    Vijay
    Follow me on twitter @vijaycbe
    World Holiday Calander || My Cricket Blog

  3. #3
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thnx viju
    but this fuction is also giving me 0,0 coords
    i made
    Code:
    document.write('<iframe name="iframes" id="mydi1" style="display:none"></iframe>');
    var iframeList1=document.getElementsByTagName('iframe');
    and used this to calling the coords
    Code:
    	for(j=0;j<iframeList1.length;j++)
    	{
    		if(navigator.appName == "mozilla" || navigator.appName == "Opera" || navigator.appName == "Netscape")
    		{
    			cl=findPosCurleft(iframeList1[j])+'|'+findPosCurtop(iframeList1[j]);
    		}
    		if(navigator.appName == "Microsoft Internet Explorer")
    		{
    			cl=findPosCurleftIE(iframeList1[j])+'|'+findPosCurtopIE(iframeList1[j]);
    		}
            }
      alert("x|y - "	+	cl);
    it is working fine for firefox but still gives 0,0 coords in IE

  4. #4
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i found out some more function but they r all giving me 0,0 coords
    i also read somewhere 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

  5. #5
    I'm not a human
    Join Date
    Aug 2006
    Location
    India
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think u r calling the function before the obj is created ..(i.e) the function is called before the obj for which u are getting the left and top position is not yet created.

    if yes then the function would always return a 0,0 value.. bcoz the javascript couldn't get the obj ..

    U should try some different way or set a fixed top and left..


    A suggestion ,, try calling the function after the obj is created ,,

    i think it wont be a major time diff b/w calling the function after the html elements are created and calling the function before the loading...
    as u r only calculating all these stuffs to just display the advts.. (that would eventually loaded from some different server).
    Regards,
    Vijay
    Follow me on twitter @vijaycbe
    World Holiday Calander || My Cricket Blog

  6. #6
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well the obj is created before the function is called as i can get the offset values in firefox and opera but the problem is IE
    it always return 0,0 for all elements
    the iframe then its parent td and then table etc all have 0,0 coords when i use alert but these show correct offsets when i use firefox
    im really confused over this as already im new to javascript and my seniors have given me a problem which they cant solve themseves

  7. #7
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and they kepp on telling me they cant wait for the page to load and get the coordinates
    i tried these examples by making a new script in which pages load and then i calculate the offsets and it is working fine


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •