views:

662

answers:

4

Hi,

I try to define a live event on img tags store on a iFrame. For example, I would like obtain a simple javascript alert box when I click a image on my iFrame.

Do you know how i can define the events for the iFrame, because I would like to put a thing like $('img').live("click",function().... but only for the elements on iFrame.

Thanks for your help.

Nicolas

+2  A: 

You can do it if you

  1. make sure that the page in the iframe has its own copy of jQuery loaded [ed.: only really necessary for jQuery operations internal to the frame's page itself]
  2. from the outer document, work into the iframe document like this:

$('#iframeId').contents().find('img') // ...

Pointy
THe iframe does not need to have jquery in anyway loaded. If you know otherwise, please provide a link citing this.
Anthony
It's more important to note that the document needs to be on the *same domain* to satisfy the same origin policy: http://en.wikipedia.org/wiki/Same_origin_policy
Justin Johnson
Well yes you're probably right; when I was typing that I was thinking of my own app, wherein the code in that sub-page needs to do its own jQuery stuff too. I don't know how much jQuery handles some of IE's weirdness around moving data between pages, but that can be tricky too. Finally, lots of frameworks used to have bugs in their duck typing code, and cross-page operations would fail in weird ways sometimes - probably all fixed now though.
Pointy
A jQuery instance is strongly tied to its owner document, many operations will fail (or, more insidiously, sometimes-fail) when attempting to script from one jQuery instance into another document. One of main problems is element creation using the wrong document. Just *looking* at the elements in another document is typically OK, but as soon as you get more complicated it is very easy to end up with very confusing errors.
bobince
+1  A: 
 $("iframe").contents().find("img")

This will target images within the iFrame. But be aware that jquery will only traverse the iFrame if it is not a violation of the browser's (or jquery's) cross-site policy.

That means if the iframe is google.com, you can't touch the inner DOM.

Anthony
http://en.wikipedia.org/wiki/Same_origin_policy
Justin Johnson
Don't I mention that?
Anthony
A: 

Thanks for your help.

I'm agree with you, but in my case, img tags are dynamically added on my iFrame after his loading. Do you know a means to manage events on tags generated after iFrame generation.

Thanks for your help

sanceray3
A: 

The other solutions here are largely myopic, as what you are trying to do is fairly complicated in the underlying javascript.

I'd suggest using jquery context as well - and I'd strongly suggest waiting for the iframe to totally load or else none of the previous suggestions could work anyway...

$("#frame").ready(function () { //wait for the frame to load

  $('img', frames['frame'].document).bind("click",function(){
   alert('I clicked this img!');
  });

});

This should generally work UNLESS you update the iframe or refresh it, in which case all the event bindings will fail... and worse yet the .live events don't appear to be supported in iframes - at least not for me.

kamelkev