PDA

View Full Version : How? - pass PopUp variable back to opener



skeatt
Nov 27th, 2002, 07:54 AM
I have a main page that opens the popUp and tracks the movement of the popup window. When you close it from the main page it stores the popUps last location so that when opened again it opens in the same position.

win = window.open( url , "newWindow" ,"left=" + scnX + ",top=" + scnY + features....."

function closePopUp(name) {
if (win && !win.closed) { //check to see if window isn't already closed
storeXY() // go to function to store coordinance
alert("X= " + scnX + " Y= " + scnY ) // debugging
win.close();
}
}

Problem: close the popUp "from" the popUp, how do you pass values back to the opener so that it knows it's last position. "scnX and scnY"

<html>
<head>
<Title>PopUp Test</title>
<h2>This is Page One</h2>
<script language="javascript">

function storeXY() { // screenLeft is IE, screenX is Netscape, subtract window borders and features, could vary slightly +/- 1 pixel.
scnX = typeof window.screenLeft != "undefined" ? window.screenLeft - 4 : window.screenX; // Borders = 4
scnY = typeof window.screenTop != "undefined" ? window.screenTop - 52 : window.screenY; // Y= No features = 23, Toolbar = 52,
}

function closeMe(name) {
alert("X= " + scnX + " Y= " + scnY ) // debugging
storeXY()
window.close();
}

function showXY() {
storeXY()
alert("X= " + scnX + " Y= " + scnY ) //debugging
}

</script>
</head>
<body onload="storeXY()" >
<a href="javascript:window.closeMe()"> 3) close this window<br><br>
<a href="javascript:window.showXY()"> 3) Show scnX / scnY value<br>
</body>
</html>

glenngv
Nov 27th, 2002, 08:03 AM
you can set a global variable in the opener from the popup...

let say you have 2 global vars in the opener:
var scnX = 0;
var scnY = 0;


then in the popup:

if (opener && !opener.closed){
opener.scnX = scnX;
opener.scnY = scnY;
}

skeatt
Nov 27th, 2002, 09:23 AM
glenngv: :thumbsup:
I gave it a shot and got a few errors, mainly scnX is undefined, so I figured the popup needed a global variable definition as well so I did this (below)....and I haven't had any nasty dialogs. I figured it was safer to give the popup a different variable name just in case the different windows got mixed up...I think thats correct?

var popUpX = 0;
var popUpY = 0;

function storeXY() { // skeatt screenLeft is IE, screenX is Netscape
if (opener && !opener.closed) {
popUpX = typeof window.screenLeft != "undefined" ? window.screenLeft - 4 : window.screenX;
popUpY = typeof window.screenTop != "undefined" ? window.screenTop - 52 : window.screenY;
opener.scnX = popUpX;
opener.scnY = popUpY;
}
}

function closeMe(name) {
storeXY()
window.close();

glenngv
Nov 27th, 2002, 09:35 AM
that's correct as long as scnX and scnY are declared global in the main window.