Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
760 views
in Technique[技术] by (71.8m points)

javascript - 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:

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.

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.

wrap();
highlight('#one');
highlight('#two');
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...