Skip to content

Instantly share code, notes, and snippets.

@Kcko
Last active November 29, 2019 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kcko/3c24de5df0c3715895633b7c3c74e49b to your computer and use it in GitHub Desktop.
Save Kcko/3c24de5df0c3715895633b7c3c74e49b to your computer and use it in GitHub Desktop.
// https://stackoverflow.com/questions/12267010/how-can-i-detect-whether-an-iframe-is-loaded/12267180
// vanilla
window.onload=function(){
var ifr=document.getElementById('MainPopupIframe');
ifr.onload=function(){
this.style.display='block';
console.log('laod the iframe')
};
var btn=document.getElementById('click');
btn.onclick=function(){
ifr.src='https://heera.it';
};
};
// jquery
$(function(){
$('#click').on('click', function(){
var ifr=$('<iframe/>', {
id:'MainPopupIframe',
src:'https://heera.it',
style:'display:none;width:320px;height:400px',
load:function(){
$(this).show();
alert('iframe loaded !');
}
});
$('body').append(ifr);
});
});
// or
$(function(){
$('#MainPopupIframe').load(function(){
$(this).show();
console.log('iframe loaded successfully')
});
$('#click').on('click', function(){
$('#MainPopupIframe').attr('src', 'https://heera.it');
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>test iframu</h1>
<iframe onload="resizeIframe(this)" src="http://localhost/xxx/b.php" frameborder="1"></iframe>
<script type="text/javascript">
function resizeIframe(iframe) {
iframe.height = iframe.contentWindow.document.body.scrollHeight + "px";
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment