Wait for iframe to load in JavaScript

本小妞迷上赌 提交于 2019-12-17 07:31:20

问题


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.


回答1:


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.




回答2:


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...




回答3:


try this one...

$('iframe').each(function() { 
    $(this).ready(function() {
        $('#result').html("ready");
    });
});


来源:https://stackoverflow.com/questions/1463581/wait-for-iframe-to-load-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!