...

View Full Version : assign event handler from child to parent window



_Gecko
02-18-2005, 10:53 PM
Is it possible to assign an event handler from a child window to a parent window?

I am using something like this:

var elem = window.opener.document.createElement("input");
elem.setAttribute('type','button');
elem.setAttribute('value','Upload Image');
elem.setAttribute('name','upload');
elem.setAttribute('onClick','uploadOpen();');
window.opener.document.getElementById ("uuimage").appendChild (elem);

Kor
02-22-2005, 10:22 AM
elem.onclick=uploadOpen;
or if u need more functions or to pass a paramater:
elem.onclick=function(){uploadOpen();anotherfunction(parameter)};

_Gecko
02-23-2005, 12:54 AM
I tried that before posting. Does not work. I read a comment somewhere about not being able to attach events from child to parent windows. Can anyone verify this?

Kor
02-23-2005, 09:58 AM
Then try seting the event handler after the appending

var root = window.opener.document.getElementById ("uuimage");
var elem = window.opener.document.createElement("input");
elem.setAttribute('type','button');
elem.setAttribute('value','Upload Image');
elem.setAttribute('name','upload');
elem.setAttribute('id','upload');
root.appendChild (elem);
root.getElementById('upload').onclick=function(){opener.uploadOpen()};

liorean
02-23-2005, 10:09 AM
_Gecko:

First, what document does the uploadOpen function reside in? Second, can you show us it's source?

Kor
02-23-2005, 10:33 AM
Liorean is right. I presumed that the function is on opener... If so, the code will definetely work. (i mean the refrences are correct, but I don't know if the goal is correct. :D

_Gecko
02-25-2005, 10:40 PM
Thanks for the help. I have already solved the problem, but out of curiosity I am goign back to see if this does in fact work.

The uploadOpen function is indeed in the opener.

The way I solved the problem is to do a refresh of the opener. I read someone's comment somewhere that they lost any values entered into a form on the opener page when a refresh was forced. The way I solved this problem was to capture all fields on the opener form to variables, then do a parent refresh like so:

window.opener.document.location = window.opener.document.location;

then rebuild all the opener form values via setTimeout() calls.
Ex: setTimeout("window.opener.document.MYFORM.__MYFIELD__.value = stored_value_for__myfield__",1250);

then after all fields are populated, do a setTimeout("self.close()",1250);

_Gecko
02-25-2005, 11:26 PM
Can you verify that this does in fact not work:

test.html:
<html><head><title>test</title></head>
<body><a href="#" onClick="uploadWin = window.open('test2.html', 'winop', 'width=500,height=200,left=300,top=250,scrollbars=auto,statusbar=yes,resizable=yes');">this is a test of the event handler</a><br><div id="uuimage">&nbsp;</div></body></html>


test2.html:
<html><head><title>test</title>
<script language="JavaScript">
function test() {
var root = window.opener.document.getElementById ("uuimage");
var elem = window.opener.document.createElement("input");
elem.setAttribute('type','button');
elem.setAttribute('value','Upload Image');
elem.setAttribute('name','upload');
elem.setAttribute('id','upload');
root.appendChild (elem);
root.getElementById('upload').onclick=function(){alert('x')};
self.close();
}
</script>
</head>
<body onLoad="test();"></body></html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum