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
Add a ment  | 

2 Answers 2

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