3

I have a click event that loads an iframe, which is followed by a .load(function() for the iframe. The problem is the load function gets fired twice for each click event. Can someone explain me the reason why this is happening? And how can it be prevented?

$(document).ready(function() {
    $('.Popup').click(function() {
        $('#myiframe').attr('src', $(this).attr('href')).load(function() {
            alert('done loading')
        })
        return false;
    });
});​
j08691
  • 204,283
  • 31
  • 260
  • 272
Newton
  • 245
  • 1
  • 5
  • 11
  • 5
    You can't trust the iframe load event. Find [another way](http://stackoverflow.com/questions/5788499/jquery-iframe-load-event) to do it. – Andy Ray Jun 26 '12 at 17:39

1 Answers1

5

The load event will fire when the iframe is created, then again when the src is set. The load event is pretty sketchy across different browsers so definitely do plenty of testing.

Jay Tomten
  • 1,657
  • 1
  • 14
  • 23
  • 3
    This is true. From [jquery load event](http://api.jquery.com/load-event/) ... ***Note: The .live() and .delegate() methods cannot be used to detect the load event of an iframe. The load event does not correctly bubble up the parent document and the event.target isn't set by Firefox, IE9 or Chrome, which is required to do event delegation.*** – efesar Jun 26 '12 at 17:45