Alireza Noori
Alireza Noori

Reputation: 15243

jquery event as soon as iframe content changes

I have a function which changes content of the iframe (for instance change the URL of the images). Currently, I bind a function to iFrame's load. Something like:

$('iframe#frame').load(function() { });

The problem is, when I run this and change the src attribute of the iframe, the function runs after all the elements are loaded and then changes it. I want to be able to run the code as soon as the HTML content of the iFrame is loaded to prevent the extra loading time. Is there any way I can achieve this?

Upvotes: 1

Views: 4742

Answers (1)

Fresheyeball
Fresheyeball

Reputation: 30015

Try this

  $.get('https://google.com', function(data){
      var doc = $frame[0].documentElement;
      doc.open();
      doc.write(data);
      doc.close();
      $frame.trigger('ijustchangedthesrc');
  }, 'HTML');

later

  $('iframe#frame').on('ijustchangedthesrc', function(){});

Give your iframe no src to start with.

Upvotes: 2

Related Questions