Forum Moderators: open

Message Too Old, No Replies

Set an onclick for an Iframe

changing contents of iframe

         

Sathallrin

2:49 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



I am trying to set and onclick event for when the iframe is clicked on.

It would have the effect of this:
<iframe id="myiframe" name="myiframe" onClick="myfunction()" style="width:100%;height:100%;"></iframe>

This does not seem to work... would there be another way to go about doing something with the same effect.

I have a function which changes the contents of the iframe... and I want this to be called when the iframe is clicked on.

tedster

2:11 am on Jul 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You haven't shown us any code here that would change the content that is loaded into the iframe, but I'm going to take a guess that this is the area where things might be hanging up for you.

It gets a bit complicated because there are two VERY different objects involved in thinking about this. First there is the iframe element itself - which is an object of its parent page, Then there is the window that is generated by the iframe and the document that is loaded in that window.

When you use an event handler like onclick for the iframe element, you are looking at an object within the parent document. But when you load a new document, you are loading it into the new window that the iframe generates. So "myfunction()" must be very precise to target the iframe window rather than the parent document.

There are also some user agent complications I've read about. For instance in some browsers (Netscape 6 for example) the onclick event for an iframe element will only fire in on a slim margin around the iframe, and not anywhere else in the iframed document.

But my guess is that the main complication is coming from the exact definition you have of myfunction()

Sathallrin

1:04 pm on Jul 14, 2005 (gmt 0)

10+ Year Member



The problem I'm having is I cant get the onclick to fire at all in any browser. If I do something like this it works fine:

<iframe id="myiframe" name="myiframe"></iframe>
<span onClick="myfunction()">Clickable button</span>

I can keep clicking on the span and it works fine changing the iframe all the time.

What I would like to have done, is it to take effect when the iFrame is clicked on (anywhere inside it) and not have to click somewhere else on the page.