views:

362

answers:

2
+1  Q: 

jquery lost events

I would like to know if is there some jquery known behaviour that cause the lost of events handlers (in particular in iframes)?

I've a strange kind of problem. I've built a webapp composed of two iframe. First i load content in the first iframe. I add some event event handler using jquery to first iframe content dom. Everything works. On user input i load a page in the second iframe. Here too, I add some event handlers using jquery. Then the strange thing happens: jquery lost the event handlers in the first iframe. I said 'jquery lost' because if I add an event listener old way, it is still present.

A: 

You might want to use live to bind events. This way when you add new elements with the same selector it will have the event binded to them.

$("p").live("click", function(){
    $(this).after("<p>Another paragraph!</p>");
});

Every subsequent p that is added to the page will have the event binded too.

MrHus
unfortunaly, I've a quite complex webapp in first i frame, replacing all bind function with live could be difficult and somewhere not possible. Plus, I don't add content to the dom of first iframe.
Paolo Chiodi
+1  A: 

Problem solved.

The problem was caused accessing iframe2.contentWindow or iframe2.contentDocument on the second iframe, when the src of the second iframe was changed (1st time everithing work, from 2nd on causes problems) and the second frame was statically coded in the html.

To solve the problem I always remove the second iframe and recreate and append it to dom dinamically via javascript.

The problem occours only on opera 9.7 embedded for mips (not sure for the exact version)

Paolo Chiodi