How to determine when an iframe source changes?

I want to determine when the user clicks the link in iframeand changes the source iframe, because I want to resize it. I also use jQuery. What is the best way to detect this?

Actually I need something like this (this example is in jQuery, it does not work, I introduced this)

$('#iframe').live('load', function(){ alert('src is changed')});
+5
source share
2 answers

You might want to use an event onLoad, as in the following example:

<iframe src="/test.html" onLoad="alert(this.contentWindow.location);"></iframe>

iframe. , , IE5 Opera. ()

, contentWindow.location, iframe , onLoad .

+12

, : , , iframe.

var prevSrc = '';
function check() {
  var curSrc = $('#iframe').attr('src');
  if (curSrc != prevSrc) {
    // source has changed; do something
    prevSrc = curSrc;
  }
}

window.setInterval(check, 1000); // 1 sec - this can be anything you want
+3

All Articles