14

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');
Himmators
  • 14,278
  • 36
  • 132
  • 223

2 Answers2

15

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
});
MattDiamant
  • 8,561
  • 4
  • 37
  • 46
  • 4
    Actually, the reday-function doesn't work on iframes, however, the load-function also has a callback function, so your overall-apporoach will work! – Himmators Feb 26 '13 at 21:34
  • What about waiting until after multiple iframes have loaded? – jbyrd Nov 15 '16 at 13:14
  • 1
    ready function does not work on iframes. It is just to execute callbacks after DOM is fully loaded. More to say, JQuery recomends to use the $(handler) syntax in place of calling the .ready(handler) function. https://api.jquery.com/ready/ – MorgoZ Feb 14 '18 at 08:32
-9

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

  • 1
    Completely irrelevant to his question. He was asking about the iframe load event you just mention... i think he knows how to declare a function – ied3vil Jun 02 '15 at 11:16