View Full Version : Adding an event to an iframe element

06-15-2007, 05:34 AM

I'm trying to add an event handler to various elements in an IFRAME. I've adding it to images, the body of the document, etc., and none of them work.

This code would be in the top frame's <SCRIPT> tags in the <HEAD> part of the document:


In the IFRAME source document, I have this in the <HEAD> of that document:

function myfunc(s) {
return false;

If I do this:

<IMG NAME="map_image" ID="map_image" SRC="http://newlightsoftware.com/map_ca.gif" BORDER="0" onclick="return myfunc('inside');">

and click on the image, it works. If I try to add the onclick event dynamically in the parent document, it does nothing. No errors or anything...

Does anyone see what I'm doing wrong? I looked for other examples like this but did not find any.


rnd me
06-15-2007, 07:18 AM
for firefox:
for IE it is contentDocument i believe.

i am not sure you can add an event handler to a framed doc from above. you might try pushing the function up to "top".


function myfunc(s) {
return false;

06-15-2007, 09:03 AM
Hi, there,
Thanks for the reply.
Actually, this part is right:

It is just that I can't dynamically set an event handler on the element that is returned, regardless of what element comes back. In this case it happens to be an image. I know there is a way to do it, I am just not seeing it yet. Maybe by adding an event listener, but not sure.

Thanks for the post, however!


06-15-2007, 06:36 PM
An event handler is not a string but a Function object.

window.frames["map_target"].document.getElementById("map_image").onclick = function(){

06-15-2007, 07:56 PM
Very cool! That does certainly work.

In my real code I'm trying to pass the mouse event in, and if you do this:

onclick="setStartDraw(event,'selectionBox');" >

where onmousedown is an attribute of an element, the event seems to be placed for you in the first parameter, for some reason.

But in this case above, no event parameter is being given to the function definition:


I'm thinking there must be a way to get it in there...

Thanks so much for your help. I'm struggling with this a lot. But learning a lot, too, thanks to this forum.


06-15-2007, 08:01 PM

I tried this to see what would happen (just a wild guess):


and it seems to be calling the function. There is another problem going on, I guess, but I THINK that I guessed it right and that the event handler is being called.

Thanks again for the help!

06-15-2007, 08:02 PM
Try this:

window.frames["map_target"].document.getElementById("map_image").onclick = function(evt){

06-15-2007, 08:14 PM
Thanks so much Glenn. It looks like that is the way to get an event in. The errors I'm seeing now have to do with the fact that I'm dealing with js code declared inside an IFRAME (well actually external js file). So, as an experienced coder, you know that the scoping issues with variables across frames is kinda wacky. At least it seems that way to a newbie!

Thanks so much for your input. :)