Below I describe two alternative methods (A and B), you can enable the loading of the first iframe:
A) Stop loading other iframes when loading the first iframe.
You will need to set up a way to track newly added frames, for example the array newIframesshown below. You can do this by tracking newIframesor retrieving them from the DOM before the iframeLoaded()following logic runs in your function .
The following is an example of how you can change your function iframeLoaded()by going through other frames, stop loading them and hide:
function iframeLoaded(evt) {
newIframes.forEach(function (newIframe) {
if (newIframe.getAttribute('div_id') !== evt.target.getAttribute('div_id')) {
if (navigator.appName === 'Microsoft Internet Explorer') {
newIframe.document.execCommand('Stop');
} else {
newIframe.stop();
}
newIframe.setAttribute('hidden', 'true');
}
});
}
B) Promise.race(), joews.
Promises jQuery, ES6 Promise.race() Promise. , promises.
, ES6:
1) createIframePromise(), iframe.
function createIframePromise(newIframe) {
var iframePromise = new Promise(function (resolve, reject) {
newIframe.onload = function (evt) {
resolve(evt.target.getAttribute('div_id'));
}
});
return iframePromise;
}
2) , iframe promises.
var iframePromises = [];
3) addTab(), iframe, iframePromises.
newIframe.onload=iframeLoaded; , createIframePromise() # 1 .
function addTab(fileName) {
iframePromises.push(createIframePromise(newIframe));
}
4) buildFileTabs(), iframes promises iframePromises.
function buildFileTabs(evt) {
Promise.race(iframePromises).then(function (firstIframeId) {
}, function () {
});
}