Page is a not externally linkable
- Code, Content, and Presentation
-- JavaScript and AJAX
---- Iframe onload event only after an anchor onclick


birdbrain - 12:32 pm on Oct 14, 2012 (gmt 0)


Hi there Rain Lover,

The code in my previous post did get the "iframe" to work in IE8, but not the alert. :(

After a little research I have managed to overcome that obstacle...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">

<title>Go Go Go</title>

<style type="text/css">
body {
background-color:#f0f0f0;
}
#iframe_a_container {
width:800px;
height:400px;
border:1px solid #999;
margin:0 auto 20px;
background-color:#fff;
box-shadow:10px 10px 10px #666;
}
#iframe_a {
display:block;
width:100%;
height:100%;
border:0;
}
#go {
display:block;
width:50px;
line-height:30px;
border:1px solid #999;
border-radius:5px;
margin:auto;
background-color:#fff;
color:#333;
text-align:center;
text-decoration:none;
box-shadow:0 0 10px #666;
}
</style>

<script type="text/javascript">

function init(){

test=true;

document.getElementById('go').onclick=function(){
ifr=document.createElement('iframe');
ifr.setAttribute('src',this.href);
ifr.src=this.href;
ifr.setAttribute('id','iframe_a');

if(test==true){

ifr.onload=function() {
alert('Thanks for the visit!');
}
ifr.onreadystatechange=function(){
if(this.readyState=='complete'){
alert('Thanks for the visit!');
}
}
document.getElementById('iframe_a_container').appendChild(ifr);
test=false;
}
return false;
}
}

window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);

</script>

</head>
<body>

<div id="iframe_a_container"></div>

<div>
<a id="go" href="http://www.bbc.co.uk/iplayer/">Go!</a>
</div>

</body>
</html>


birdbrain


Thread source:: http://www.webmasterworld.com/javascript/4507876.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com