views:

314

answers:

1

Hello, how can I get iframe position using JS in the current iframe?

This code works in IE

var browser = navigator.appName;
if (browser == "Microsoft Internet Explorer") {
  scrollX=self.screenLeft;  //works
  scrollY=self.screenTop;  //works strange
}

How to do it in Firefox?
screenLeft, screenTop - undefined
screenX, screenY - the position of the parent window
parent.frames[self.name].screenX - the same result as screenX

Any idea, where is my iframe in FF? Thx.

+1  A: 

You could do this from within the iframe (http://www.quirksmode.org/js/findpos.html)

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
    do {
        curleft += obj.offsetLeft;
        curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
    return [curleft,curtop];
}
var topWindow = window.top;
var leftTopPosition = findPos(topWindow.document.getElementById("iframe_id"));

You must specify the iframe's ID, or reference the iframe some other way.

Luca Matteis
Object topWindow.document is inaccesible :-(