Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for iframe to load in JavaScript

I am opening an iframe in JavaScript:

righttop.location = "timesheet_notes.php";

and then want to pass information to it:

righttop.document.notesform.ID_client.value = Client;

Obviously though, that line isn't going to work until the page has fully loaded in the iframe, and that form element is there to be written to.

So, what is the best/most efficient way to address this? Some sort of timeout loop? Ideally I would really like to keep it all contained within this particular script, rather than having to add any extra stuff to the page that is being opened.

like image 424
Bill Dawes Avatar asked Sep 23 '09 01:09

Bill Dawes


People also ask

Can iframe load JavaScript?

To capture iframe load complete event with JavaScript, we can set the onload property of the iframe to a function that runs when the iframe content finishes loading. const iframe = document.

How check iframe is loaded or not in JavaScript?

To check if iframe is loaded or it has a content with JavaScript, we can set the iframe's onload property to a function that runs when the iframe is loaded. document. querySelector("iframe"). onload = () => { console.

How do you call a function after an iframe is loaded?

To call a JavaScript function when iframe finished loading, we can add an event listener for the load event. to add an iframe. to select the iframe with querySelector .


2 Answers

First of all, I believe you are supposed to affect the src property of iframes, not location. Second of all, hook the iframe's load event to perform your changes:

var myIframe = document.getElementById('righttop');
myIframe.addEventListener("load", function() {
  this.contentWindow.document.notesform.ID_client.value = Client;
});
myIframe.src = 'timesheet_notes.php';

Again, this is all presuming you mean iframe, not framesets.

like image 74
Crescent Fresh Avatar answered Oct 19 '22 21:10

Crescent Fresh


I guess you can pretty easily do this with jQuery... jQuery Home Just hook the page to the jQuery $ function ()... e.g.

$(document).ready(function() { 
    $('iframe').load(function() { 
       // write your code here....
    });
});

Have a quick look at the file uploader example here: Using iframes for multiple file uploads...

like image 8
rajesh pillai Avatar answered Oct 19 '22 21:10

rajesh pillai