简体   繁体   English

离开页面前加载iframe

[英]Load iframe before leaving the page

I have this problem: 我有这个问题:

User clicks to a link to the other page. 用户单击指向另一个页面的链接。 When this event occurs i want to load an iframe. 当发生此事件时,我想加载iframe。 I need to load that iframe before new page will start loading. 我需要先加载该iframe,然后才能开始加载新页面。

How can I do that? 我怎样才能做到这一点?

edit: 编辑:

I have code as fallow: 我有以下代码:

function letsDoThis(parameter)
{
    var iframe=document.createElement('iframe');
    iframe.src="http://xxxxxxxxx"+parameter;
    iframe.width='1';
    iframe.height='1';
    document.body.appendChild(iframe);
}
var a=$('#MY_BUTTON')[0].href;
$('#MY_BUTTON')[0].addEventListener('click',function(){letsDoThis(a);});

but in this solution iframe doesn't load at everytime (sometimes new page will start loading too fast). 但是在此解决方案中,iframe并非每次都加载(有时新页面开始加载的速度过快)。

Using jQuery, you may easily solve this: 使用jQuery,您可以轻松解决以下问题:

 $('body').on('click', 'a#MY_BUTTON', function(e) { e.preventDefault(); var url_a = $(this).attr('href'); var ifr=$('<iframe/>', { id:'DynamicIframe', src:'http://stackoverflow.com/', style:'width:1px; height: 1px;', load:function(){ console.log('iframe loaded!'); //document.location.href = url_a; alert(url_a); } }); $('body').append(ifr); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <a href='http://www.google.it/' id='MY_BUTTON' >Click!</a> 

You should change the url of the iframe passing parameters that you need and in case of more iframe and link to control, manage everything through classes and not by ID 您应该更改所需的iframe传递参数的网址,如果需要更多iframe并链接到控件,则应通过类而不是ID来管理所有内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM