Dynamically created iframe used to download file triggers onload with firebug but not without

做~自己de王妃 提交于 2019-12-02 00:47:15

问题


EDIT: as this problem is now "solved" to the point of working, I am looking to have the information on why. For the fix, see my comment below.

I have an web application which repeatedly downloads wav files dynamically (after a timeout or as instructed by the user) into an iframe in order to trigger the a default audio player to play them. The application targets only FF 2 or 3. In order to determine when the file is downloaded completely, I am hoping to use the window.onload handler for the iframe. Based on this stackoverflow.com answer I am creating a new iframe each time. As long as firebug is enabled on the browser using the application, everything works great. Without firebug, the onload never fires. The version of firebug is 1.3.1, while I've tested Firefox 2.0.0.19 and 3.0.7. Any ideas how I can get the onload from the iframe to reliably trigger when the wav file has downloaded? Or is there another way to signal the completion of the download? Here's the pertinent code:

HTML (hidden's only attribute is display:none;):

<div id="audioContainer" class="hidden">
</div>

JavaScript (could also use jQuery, but innerHTML is faster than html() from what I've read):

waitingForFile = true; // (declared at the beginning of closure)
$("#loading").removeClass("hidden");
var content = "<iframe id='audioPlayer' name='audioPlayer' src='" +
    /path/to/file.wav + "' onload='notifyLoaded()'></iframe>";
document.getElementById("audioContainer").innerHTML = content;

And the content of notifyLoaded:

function notifyLoaded() {
    waitingForFile = false; // (declared at beginning of the closure)
    $("#loading").addClass("hidden");
 }

I have also tried creating the iframe via document.createElement, but I found the same behavior. The onload triggered each time with firebug enabled and never without it.

EDIT: Fixed the information on how the iframe is being declared and added the callback function code. No, no console.log calls here.


回答1:


Old question but for future reference: As far as my experience onLoad is not called for file downloads. A way to solve it is to use cookies like they do here http://gruffcode.com/2010/10/28/detecting-the-file-download-dialog-in-the-browser/




回答2:


Here's an example that works for me, without Firebug open (tested in FF 3.6.2 Mac): http://www.jsfiddle.net/Kukry/

I'm using the jQuery .load() event instead of onload.

var iframe = $("<iframe/>").load(function () {
    alert("loaded");
}).attr({
    src: "http://code.jquery.com/jquery-1.4.2.min.js"
}).appendTo($("#thediv"));

Note that I'm loading a JavaScript file, not an audio file, so that might make a difference.




回答3:


Maybe you call some Firebug internal function, like console.log(), somewhere? In that case, Firefox will threw an exception which can stop the execution if Firebug is not active.



来源:https://stackoverflow.com/questions/2456695/dynamically-created-iframe-used-to-download-file-triggers-onload-with-firebug-bu

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