开发者

iframe contents change event?

开发者 https://www.devze.com 2022-12-22 14:51 出处:网络
How can I detect when the iframe content has changed and do something upon开发者_如何学运维 that change pseudo code below

How can I detect when the iframe content has changed and do something upon开发者_如何学运维 that change pseudo code below

 $('#waframe').contents().change(function(){
   //do stuff
  });


Well, the browser seems to generate a "load" event on an <iframe> element in the context of the containing page. The "load" fires when the "src" is changed; not sure whether it fires when the iframe changes itself.

edit: yes it does seem to fire when the page reloads "internally"

Thus you might try:

$('iframe#yourId').load(function() {
  alert("the iframe has been loaded");
});


Am doing a litle trick (dirt but it works) :) to display a loading object

function changeDet(old){ //detect src changes
    if($("iframe#ifID").attr('src') != old){
        $("iframe#ifID").width(0); //error if i hide ...?
    }

    setTimeout('changeDet("' + $("#db").attr('src') + '")',10);
}

var df = $('#iframe#ifID');

df.load(function(){ //content loaded
setTimeout('$("#db").width(var_default_width);',100);
});

changeDet();

$(function(){   
    //jq stuffs...
})
</script>


You can use this to fire event on change.. Works on IE8 and firefox

if(document.addEventListener){
    yourIFrameObject.contentWindow.document.addEventListener('keyup', yourFunction, false);
   // window.frames[0].document.addEventListener('keyup', yourFunction, false);
}
else{
//for IE8
    yourIFrameObject.contentWindow.document.attachEvent('onkeyup', yourFunction);
}

You could use a mouseup statement if necessary

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号