/    Sign up×
Community /Pin to ProfileBookmark

Iframe onload event only after an anchor onclick

Sample code:

[CODE]<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<iframe name=”iframe_a” onload=”alert(‘Thanks for the visit!’);”></iframe>
<a href=”http://www.example.com/” target=”iframe_a”>Go!</a>
</body>
</html>[/CODE]

I’d like to see the alert message after clicking on the link and when the iframe finishes loading. But now it appears on the initial page load, too. How can I achieve it?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@cootheadOct 14.2012 — Hi there Rain Lover,

this code has been tested in IE8, IE9, Firefox 16.o.1, Opera 12.02 and Safari 5.1.7...
[color=navy]&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

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

&lt;title&gt;Go Go Go&lt;/title&gt;

&lt;style type="text/css"&gt;
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;
}
&lt;/style&gt;

&lt;script type="text/javascript"&gt;

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

ifr.onload=function() {
alert('Thanks for the visit!');
}
document.getElementById('iframe_a_container').appendChild(ifr);
return false;
}
}

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

&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;div id="iframe_a_container"&gt;&lt;/div&gt;

&lt;div&gt;
&lt;a id="go" href="http://www.bbc.co.uk/"&gt;Go!&lt;/a&gt;
&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;
[/color]

[i]coothead[/i]
×

Success!

Help @Rain_Lover spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...