2014-05-06 179 views
0

處理應該通過將圖像放入iframe中預加載圖像的腳本。點擊一個按鈕後,它將預載更多圖像並停止加載之前的iframe(不再需要)。

我收到兩個錯誤,雖然我想不通:通過iframe預加載圖像錯誤

  1. 資源解釋爲文檔,但與MIME類型 圖像/ JPEG

  2. 遺漏的類型錯誤轉移:無法讀取屬性「contentWindow」的 undefined

後一個錯誤意味着我的腳本沒有完全工作,所以我的問題是:我的代碼有什麼問題? 這裏有一個jsfiddle

function preload(arrayOfImages) { 
    $(arrayOfImages).each(function(){ 
     image_iframe[this] = $('<iframe />').appendTo('body')[0]; 
     image_iframe[this].onload = function() { 
      console.log('Image Cached!'); 
      this.remove(); 
     }; 
     image_iframe[this].src = image_url[this]; 
    }); 
} 

var image_iframe = []; 
var image_url = []; 

image_url[1] = '/image.jpg'; 
image_url[2] = '/another-image.jpg'; 
image_url[...] = 'etc'; 

preload([2, 3]); 
var current_url = 1; 

function nextImage() { 
    if(current_url <= 50) { 
     current_url = current_url + 1; 
     preload([current_url + 1, current_url + 2]); 
     for (var z=1;z<current_url;z++) { 
      image_iframe[z].contentWindow.stop(); 
      image_iframe[z].execCommand("Stop", false); 
     } 
    } else { 
     window.location.replace("/"); 
    } 
} 

$('#remove').click(function(){ 
    nextImage(); 
}); 

我使用的I幀的原因是因爲我需要一種方法來停止加載特定的圖像。
Cancel single image request in html5 browsers

編輯:我認爲這個問題是我如何定義image_iframe[]
EDIT2:天哪忘了,我開始了我的預載在2,這意味着我的循環(從1開始)給出了一個未定義錯誤。

回答

0

Resource interpreted as Document but transferred with MIME type image/jpeg

固定本身

Uncaught TypeError: Cannot read property 'contentWindow' of undefined

我開始與陣列2中的預加載,但由1