简体   繁体   English

等待iframe加载,然后触发下一个功能?

[英]Wait for iframe to load before next function is triggered?

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. 本质上,它将当前页面加载为iframe。 I need it in order to keep the javascript running even when links are clicked on the page. 我需要它以便即使在页面上单击链接也能保持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. 当运行换行时,我想开始处理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. 对于应用程序的结构,我需要从wrap函数的ouside或使用我传递给wrap函数的参数来完成。 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. 这就是我理想的样子。

wrap();
highlight('#one');
highlight('#two');

jQuery can use the ready function on iframes, just the same as it can with the document. jQuery可以在iframe上使用ready函数,就像在文档中一样。 Put it inside your highlight function, and it will run after the iframe is done loading. 将其放在突出显示功能中,它将在iframe加载完成后运行。

$("iframe").ready(function (){
    // do something once the iframe is loaded
});

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加载后执行

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

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