如何在加载前获取Iframe事件? [英] How can I get Iframe event before load?
问题描述
在我的网站中,我在iframeB中使用iframeA,并且当iframeA更改其内容时,我必须设置src。我只能使用onload事件设置它,但是在加载站点时会调用它。我正在寻找一些事件或触发器,这有助于我在开始加载之前检测位置/ src更改。在src设置之前,我不想等待整个页面加载。我没有直接访问iframeA(只是下面的脚本)
In my site, I use an iframeA in an iframeB, and, when the iframeA changes it's content I have to set the src. I can set it only with the onload event, but this called when the site is loaded. I am looking for some event or trigger, that helps me detect the location/src change before it starts loading. I don't want to wait the whole page load, before the src set. I have no direct access to iframeA (just the script below)
一些代码:
var myframe = document.getElementById('frameB').contentWindow.document.getElementById('frameA');
myframe.onload=function (funcname) {...};
推荐答案
什么会改变iframe的来源?如果您有权访问该代码,那么您可以执行 onload
函数中的任何操作。
What will be changing the source of the iframe? If you have access to that code then you can do whatever is in your onload
function then.
如果有链接有目标
属性设置为iframe,这就是源的变化方式,然后你可以点击链接点击:
If a link has it's target
attribute set to the iframe and that is how the source is changing then you can hi-jack the link clicks:
$('a[target="frameB"]').bind('click', function () {
//run your onload code here, it will run as the iframe is downloading the new content
});
另外,只需注意,您可以绑定在
事件:
Also, just a side-note, you can bind an event handler for the load
event in jQuery like this:
$('#frameB').bind('load', function () {
//run onload code here
});
UPDATE
SITE - > frameB - > frameA
UPDATE
SITE -> frameB -> frameA
$("#frameB").contents().find("#frameA").bind('load', function () {
//load code here
});
这会选择 #frameB
元素(即获取当前的顶级DOM),获取它的内容,找到 #frameA
元素,然后绑定 load $的事件处理程序c $ c> event。
This selects the #frameB
element (that is in the current top level DOM), gets it's contents, finds the #frameA
element, and then binds an event handler for the load
event.
请注意,此代码必须在 #frameB
加载<$后运行c $ c> #frameA 元素已经存在于它的DOM中。这样的事情可能是个好主意:
Note that this code must be run after #frameB
is loaded with the #frameA
element already present in it's DOM. Something like this might be a good idea:
$('#frameB').bind('load', function () {
$(this).contents().find('#frameA').bind('load', function () {
//run load code here
});
});
UPDATE
到hi-jack链接在 #frameB
元素中:
$('#frameB').contents().find('a[target="frameA"]').bind('click', function () {
/*run your code here*/
});
这将找到 #frameB
将目标
属性设置为 frameA
的元素,并添加单击
事件处理程序。
This will find any link in the #frameB
element that has its target
attribute set to frameA
and add a click
event handler.
而且,只有在 #frameB
iframe元素已加载时才会起作用(或者至少得到 document.ready
事件),这样你就可以选择它的元素。
And again, this will only work if the #frameB
iframe element has loaded (or atleast gotten to the document.ready
event) so you can select it's elements.
这篇关于如何在加载前获取Iframe事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!