<!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>
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?
function done() {
//some code after iframe has been loaded
}
function WaitForIFrame() {
if (iframe.readyState !== "complete") {
setTimeout(WaitForIFrame, 200);
} else {
done();
}
}
var iframe = document.getElementById('iframe_a');
WaitForIFrame();
You want the WaitForIFrame() command to be issued only after clicking on the link. So, give that link a suitable id, and move the WaitForIFrame() command inside of an onclick event for the link, such as this:
var link = document.getElementById('...');
link.onclick = function () {
WaitForIFrame();
};
Thank you so much for your time and walking me through the steps above.
I finally came up with something simpler and would be happy to get your review:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<iframe id="iframe_a" name="iframe_a" onload="if(this.className=='active'){alert('Thanks for the visit!');};"></iframe>
<a href="http://www.example.com/" target="iframe_a" onclick="document.getElementById('iframe_a').className='active';">Go!</a>
</body>
</html>
Yes that also works, but you don’t want to have scripting code embedded within your HTML code. You don’t embed CSS styles within your HTML code too do you?
function waitForIFrame() {
if (this.className === 'active') {
alert('Thanks for the visit!');
}
};
function initIFrameWait() {
document.getElementById('iframe_a').className = 'active';
};
document.getElementById('iframe_a').onload = waitForIFrame;
document.getElementById('link_a').onclick = initIFrameWait;
The next issue is about why a class name is being used. If it’s not going to be used to affect any CSS styling of the iframe element, then you really should not allow the potential for unintended side-effects later on. Just Move the onload event assignment inside of the onclick event, so that you don’t need to mess around with class-based state-checking systems.
function waitForIFrame() {
alert('Thanks for the visit!');
};
function initIFrameWait() {
document.getElementById('iframe_a').onload = waitForIFrame;
};
document.getElementById('link_a').onclick = initIFrameWait;
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
var clicked = false;
function activate() {
clicked = true;
}
function pop() {
if (clicked) {
alert('Thanks for the visit!');
};
}
</script>
</head>
<body>
<iframe name="iframe_a" onload="pop();"></iframe>
<a href="http://www.example.com/" target="iframe_a" onclick="activate();">Go!</a>
</body>
</html>
Just Move the onload event assignment inside of the onclick event, so that you don’t need to mess around with class-based state-checking systems.
function waitForIFrame() {
alert('Thanks for the visit!');
};
function initIFrameWait() {
document.getElementById('iframe_a').onload = waitForIFrame;
};
document.getElementById('link_a').onclick = initIFrameWait;
It makes sense and was the first thing I tried, but it doesn’t work in IE8.