...

View Full Version : pass event to parent



nimexx
08-10-2005, 09:18 PM
How can I pass an event from a frame to its parent? I want to be able to assign the event from the parent (document.frames(0).document.onclick = go) but I cant even get the function to work right when it is assigned in the frame itself. The function in the parent is getting called but the event isnt getting through. It works on the inline call on the image in the frame but I cant get it to work via script.

Frame:

<html>
<head>
<script language="JavaScript1.2">
//document.onclick = parent.go;
document.onclick = parent.go(event);
//-->
</script>
</head>
<body>
<img name="image1" src="images/1.jpg" width="100" height="100" onclick="parent.go(event)">
</body>
</html>

Parent:

<html>
<head>
<script language="JavaScript1.2">
document.onclick = go;
//document.frames(0).document.onclick = go;

function go(event){
alert(event.type);
return false;
}
//-->
</script>
</head>
<body>
<iframe id="frame1" name="frame1" src="frame1.htm" width="240" height="175" border="1" frameborder="1"></iframe>
</body>
</html>

martin_narg
08-10-2005, 10:13 PM
i've not tested this but theoretically it should work:


document.onclick = function(e) {
parent.go(e || window.event);
}


hope this helps

m_n

nimexx
08-11-2005, 07:03 PM
Yes thanks,
document.onclick = function(e) { parent.go(e || window.event); } does work in the frame. Now is there a way to set this from the parent?

I would have thought this would work:


<html>
<head>
<script language="JavaScript1.2">
function setframe(){
document.frames(0).document.onclick = function(e) { parent.go(e || window.event); }
}

function go(event){
alert(event.type);
return false;
}
//-->
</script>
</head>
<body>
<iframe id="frame1" name="frame1" src="frame1.htm" width="240" height="175" border="1" frameborder="1" onload="setframe()"></iframe>
</body>
</html>

I had to call it from the onload event of the iframe, I assume its because the frame object has to exist before I can set it.

When I run it I get an object required error in the go function. The function is getting called by the frame but the event isnt getting passed or something.

Im definately confused because if I look at the DOM, the value of document.frames(0).document.onclick is identicable wether I do it from the frame or the parent but it works one way and not the other. Am I missing something?

martin_narg
08-12-2005, 10:36 AM
Again untested but theoretically:

in parent:


window.onload = function() {
document.frames[0].contentWindow.document.onclick = function(e) {
parent.go(e || window.event);
}
}


Hope this helps

m_n

nimexx
08-12-2005, 09:33 PM
Couldnt get that one to work, I kept getting a document.frames.0.contentWindow.document is null or not an object error. I think its becuase the frame doesnt exist at window.onload.

I also tried replacing
document.onclick = function(e) { parent.go(e || window.event); } in the parent with
document.frames[0].contentWindow.document.onclick = function(e) {
parent.go(e || window.event); but then I get an object doesnt support this property or method error.

Im also wondering that even if I get it to work from the parent correctly, what happens if the frame gets refreshed? Would the document.onlick of the frame get cleared if it was only assigned by the javascript in the parent?

Maybe Im stuck doing it in the frame.

Thanks for the help though, Im getting closer.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum