I'm creating iframes dynamically to display documents obtained via a url. Each document is in an iframe which is, in turn, in a div. I have a tab bar that allows any document to be displayed while hiding the rest, a typical tabbed page.
I would like to display the div containing the iframe that receives the first response, i.e. display the first that loaded and hide the rest. There is no way to predict which will load first so I need a way to detect the first and display it and hide all of the rest.
I think I can do this by having the iframe onload function check a global boolean that is set to true when the first onload handler runs.
I'm not sure why, but this feels error prone. Is there a better way to do this?
var firstDocumentReceived = false ;
function buildFileTabs(evt) {
firstDocumentReceived = false ;
var files = evt.target.files; // files is a list of File objects.
for (var i = 0, f; f = files[i]; i++) {
addTab ( files[i].name );
}
// create a div to display the requested document
function addTab ( fileName ) {
var newDiv = document.createElement("div");
newDiv.id = fileName.replace(".","_") + newRandomNumber() ;
// create an iframe to place in the div
var newIframe = document.createElement("iframe");
newIframe.onload=iframeLoaded ;
newIframe.setAttribute("seamless", true );
newIframe.setAttribute("div_id" , newDiv.id) ;
newIframe.src = url ; // the iframe will contain a web page returned by the FDS server
// nest the iframe in the div element
newDiv.appendChild(newIframe) ;
// nest the div element in the parent div
document.getElementById("documentDisplay").appendChild(newDiv) ;
...
function iframeLoaded ( event ) {
if ( firstDocumentReceived === false ) {
firstDocumentReceived = true ;
// show the div associated with this event
} else {
// hide the div associated with this event
}