Entry
How do I open a window at the position of an element in the page?
Nov 8th, 2001 07:54
Martin Honnen,
Here is a solution that works with NN4 if you use an anchor as the
element (or another element like an img that has x/y properties) and
with NN6 for all elements.
I can't solve that for IE as IE doesn't provide the outer dimensions of
a window.
<html>
<head>
<script type="text/javascript">
function openWinAtElement(element) {
var x, y;
if (document.layers) {
x = element.x + window.screenX + window.outerWidth - window.innerWidth;
y = element.y + window.screenY + window.outerHeight -
window.innerHeight;
}
else if (document.all) {
var coords = getPageCoords(element);
x = coords.x + window.screenLeft + 5;
y = coords.y + window.screenTop + 130;
}
else if (document.getElementById) {
var coords = getPageCoords(element);
x = coords.x + window.screenX + window.outerWidth - window.innerWidth;
y = coords.y + window.screenY + window.outerHeight - window.innerHeight;
}
return window.open('http://JavaScript.faqts.com', 'faqts', 'left=' + x
+ ',top=' + y + ',width=400,height=400,scrollbars,resizable');
}
function getPageCoords (element) {
var coords = {x: 0, y: 0};
while (element) {
coords.x += element.offsetLeft;
coords.y += element.offsetTop;
element = element.offsetParent;
}
return coords;
}
</script>
</head>
<body onload="openWinAtElement(document.anchors.anAnchor)">
<script type="text/javascript">
var rand = Math.floor(Math.random() * 15) + 5;
for (var i = 0; i < rand; i++)
document.write(i + ' Kibology<br \/>');
</script>
<a name="anAnchor" id="anAnchor">anchor to place window at</a>
</body>
</html>