jQuery 在触发下一个函数之前等待 iframe 加载?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15099537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wait for iframe to load before next function is triggered?
提问by Himmators
First off: I know things should be run asynchronously if possible.
首先:我知道如果可能的话,事情应该异步运行。
I have a function, called wrap:
我有一个名为 wrap 的函数:
essentially it loads the current page as an iframe. I need it in order to keep the javascript running even when links are clicked on the page.
本质上,它将当前页面加载为 iframe。我需要它以便即使在页面上单击链接时也能保持 javascript 运行。
function wrap(){
event.preventDefault();
var pathname = window.location.pathname;
$('body').html('<iframe id="wrapper" src="'+ pathname +'" >')
$('iframe').load(function (){
//this is where the magic outght to happen
});
}
When the wrap is run, i want to start manipulating the contents of the iframe. For the structure of the app, I would like need to do this from ouside of the wrap-function or with parameters that I pass in to the wrap function. I do this with a series of functions that execute animations, play sounds etc. (Stuff that takes time and should also be executed sequentially). This Is what i Would Ideally like it to look like.
当包装运行时,我想开始操作 iframe 的内容。对于应用程序的结构,我需要从 wrap-function 外部或使用我传递给 wrap 函数的参数来执行此操作。我用一系列执行动画、播放声音等的函数来做到这一点(需要时间的东西也应该按顺序执行)。这就是我最喜欢的样子。
wrap();
highlight('#one');
highlight('#two');
回答by MattDiamant
jQuery can use the ready function on iframes, just the same as it can with the document. Put it inside your highlight function, and it will run after the iframe is done loading.
jQuery 可以在 iframe 上使用 ready 函数,就像对文档一样。把它放在你的高亮函数中,它会在 iframe 加载完成后运行。
$("iframe").ready(function (){
// do something once the iframe is loaded
});
回答by Mitermayer Reis
You could create a function:
你可以创建一个函数:
function iframeready() {
// code after iframe is loaded
}
And pass this function as a callback of iframe load event so this way your code would execute after the iframe was loaded
并将此函数作为 iframe 加载事件的回调传递,这样您的代码将在 iframe 加载后执行