admin管理员组文章数量:1420929
I need to trigger a function when the iframe loads a second time (the user clicks some link in the iframe). When the iframe page changes I can set the window location to the iframe src.
What I thought would be best is to set an onLoad attribute on the iframe element after the first load. I guess I would need the live event to tell when the iframe has been created, since it's dynamic.
This is what I had in mind:
$(document).ready(function() {
$('#tenant_login').fancybox({type:'iframe'});
$('#tenant_login').click(function() {
$('#fancybox-frame').attr('onload', function() {
window.location = $('#fancybox-iframe').attr('src');
});
});
});
If it matters the iframe is not cross domain.
I need to trigger a function when the iframe loads a second time (the user clicks some link in the iframe). When the iframe page changes I can set the window location to the iframe src.
What I thought would be best is to set an onLoad attribute on the iframe element after the first load. I guess I would need the live event to tell when the iframe has been created, since it's dynamic.
This is what I had in mind:
$(document).ready(function() {
$('#tenant_login').fancybox({type:'iframe'});
$('#tenant_login').click(function() {
$('#fancybox-frame').attr('onload', function() {
window.location = $('#fancybox-iframe').attr('src');
});
});
});
If it matters the iframe is not cross domain.
Share Improve this question edited Apr 11, 2013 at 9:00 Barney 16.5k5 gold badges65 silver badges80 bronze badges asked Aug 19, 2010 at 1:37 BenbobBenbob 14.3k19 gold badges83 silver badges114 bronze badges 5- "onload" should be lowercase. – Paul Schreiber Commented Aug 19, 2010 at 1:40
- You are trying to have users click around on some other web page via an iframe, then at some point, wherever they are, redirect them to that same URL in a regular window? – Andrew Atkinson Commented Aug 19, 2010 at 5:11
- @Andy Atkinson Yes that's exactly what I'm trying to do. What I noticed is the src of the iframe never changes. I just used a target="_top" attribute on the particular link I wanted to open in a regular window it solved the problem. I guess I could bind an event to the iframes DOM somehow, but "_top" was easier. – Benbob Commented Aug 19, 2010 at 5:35
- Hey Keyo, can you explain how you solved your problem and mark your answer as accepted? This is drawing a lot of traffic and Googles fairly highly for iframe onload related problems — it'd be good to see the solution! – Barney Commented Apr 11, 2013 at 9:01
- I've added an answer. I think Stefan's answer is probably more useful for solving the original question I asked. – Benbob Commented Apr 11, 2013 at 10:52
2 Answers
Reset to default 2$("#fancybox-iframe").load(function(){
window.location = $('#fancybox-iframe').attr('src');
});
This will allow you to redirect the parent window when the iframe loads a different page.
If you have control of the iframe content, as I did setting a target="_top"
attribute on the hyperlink means the browser will open the link in the browser window rather than the iframe.
Main document:
<html>
<body>
<h1>The Main Page</h1>
<iframe src="myIframe.html" />
</body>
<html>
Iframe document:
<h2>The IFRAME</h2>
<a href="something.html" target="_top" >
This will open in the browser window rather than any iframe it exists in.
</a>
If you can't put target="_top"
on the links of the iframe DOM you will need to capture the click events with javascript from the parent DOM.
本文标签: javascriptjQuery set iFrame onLoad attributeStack Overflow
版权声明:本文标题:javascript - jQuery set iFrame onLoad attribute - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745341103a2654264.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论