Display window when Iframe changes/loads
I'm using this script that shows a little warning when a user clicks on a link:
<script>
var disclaimer="This link is not our responsibility - click ok to continue"
iframe.onload=function() {
var links = document.links;
for (var i=0, n=links.length;i<n;i++) {
links[i].onclick=function() { return confirm(disclaimer);}
}
}
</script>
I have an iframe in the site, I want to trigger the 开发者_如何学Csame window when the user clicks on a link on the iframe. Is there a way to do it with plain javascript?
Thanks a lot
You can use the parent keyword to invoke the current window instead of the iframe window.
For example, (in the iframe)
parent.confirm(disclaimer);
Assuming the protocol, domain and port matches your page, you can access your iframe
's document with the contentDocument
property (which is the standard, but you may need to use contentWindow.document
instead).
var iframe = document.getElementsByTagName('iframe')[0],
iframeDocument;
if ('contentDocument' in iframe) {
iframeDocument = contentDocument;
} else {
iframeDocument = iframe.contentWindow.document;
}
精彩评论