Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting location relative to window with frames

Status
Not open for further replies.

NorthStarDA

IS-IT--Management
Mar 16, 2004
614
US
i need to get the location of an element relative to the entire window, but the element is inside an iframe- i'm using the functions below to loop through offsetParents, but iframes apparently do not have an offsetParent. so they return the location relative to the iframe the element is in, not the main outer window- how can i get that?

Code:
function findPosX(obj)
{
	var x1 = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			x1 += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		x1 += obj.x;
	return x1;
}

function findPosY(obj)
{
	var y1 = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			y1 += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		y1 += obj.y;
	return y1;
}


=========================================
I have not failed. I've just found 10,000 ways that won't work.
Thomas A. Edison
 
does anybody know? i'm also having this issue with getting mouse coordinates relative to the main window, not an iframe- appreciate any help here.


=========================================
I have not failed. I've just found 10,000 ways that won't work.
Thomas A. Edison
 
You'll need to find the location of the iframe and add that offset to the location within the iframe.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
old post, but i think i found a solution...

a little client math for the iframe relative to the overall
it works in my situation with parent > one iframe

Code:
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;
}

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 hideContact()
{
var elem = parent.document.getElementById('contactDiv');
if (elem) 
{
  if (elem.style.display != 'block') 
  {
    elem.style.display = 'none';
    elem.style.visibility = 'hidden';
  } 
  else
  {
    elem.style.display = 'none';
    elem.style.visibility = 'hidden';
  }
}
}
  
function showContact(clicked,product)
{
var elem = parent.document.getElementById('contactDiv');
if (elem) 
{
  if (elem.style.display != 'block') 
  {
    var newX = findPosX(clicked);
    var newY = findPosY(clicked);
    // if lower on page, raise it up a tad
    if (newY > 700){
        newY = newY - 300;
    }
    [b]var ifrmX = parent.document.body.clientWidth - document.body.clientWidth;
    var ifrmY = document.body.clientHeight - parent.document.body.clientHeight;
    elem.style.top = newY + ifrmY + 'px';
    elem.style.left = newX + ifrmX + 'px';[/b]
    elem.style.display = 'block';
    elem.style.visibility = 'visible';
    parent.document.getElementById('txtProduct').value = product;
  } 
  else
  {
    elem.style.display = 'none';
    elem.style.visibility = 'hidden';
  }
}
}
 
small iframes failed with a negative Y value... so i did this with better results...


var ifrmX = parent.document.body.clientWidth - document.body.clientWidth;
var ifrmY = document.body.clientHeight - parent.document.body.clientHeight;
if (ifrmY < 0){ ifrmY = 0;}
elem.style.top = newY + ifrmY + 'px';
elem.style.left = newX + ifrmX + 'px';
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top