2011-08-16 10 views
2

,所以我有這個腳本:image onerror沒有在ie7註冊?

<script type="text/javascript"> 
     $(window).load(function() { 
      var imagePath = new String($('.give').css("background-image").toString()); 
      if (imagePath == "none") { 
       $('.give').css("text-indent", "0px"); 
      } 
      else if (imagePath != null) { 
       var tempImage = new Image(); 
       tempImage.onerror = function() { 
        if (!tempImage.complete) { 
         $('.give').css("text-indent", "0px"); 
        }    
       } 
       imagePath = imagePath.split("images/")[1]; 
       imagePath = "images/" + imagePath.split(")")[0]; 
       imagePath = imagePath.split('"')[0]; 
       tempImage.src = imagePath; 
      } 
     }); 
    </script> 

我需要檢查,如果圖像被禁用在瀏覽器中,如果他們是我需要把文本帶回在屏幕上,因此該應用可以在沒有圖像可以使用。第一個if語句處理chrome和firefox,因爲由於某些原因,如果圖像被禁用,它們會爲背景css屬性返回「none」。而不是ie7的情況。第二個if語句試圖處理ie的情況,但由於某種原因,.onload或.onerror都被解僱了。

有沒有人遇到過這個可以幫忙?或者指向正確的方向?我已經嘗試了很多方法來做到這一點。但我似乎無法讓它工作。

+0

你嘗試tempImage.onerror =函數(){警報( '錯誤' )}看到它不是沒有設置的.complete – mplungjan

+0

是的,我曾嘗試過,但我重試只是爲了理智,並且onerror仍然沒有被調用。 –

回答

2

這是我的解決方案:

<script type="text/javascript"> 
    detectImageEnabledMode({ 
     onDetectImageIsDisabled:function(){ 
      alert('disabled'); 
     }, 
     onDetectImageIsEnabled:function(){ 
      alert('enabled'); 
     } 
    }); 
    function detectImageEnabledMode(options){ 
     /* define disabled/enabled actions */ 
     var actionCounter = 0; 
     var enabledAction = (options.onDetectImageIsEnabled && typeof(options.onDetectImageIsEnabled)=='function')?options.onDetectImageIsEnabled:function(){}; 
     var enaledActionRun = function(){ 
      if(actionCounter) return; 
      actionCounter++; 
      enabledAction(); 
     } 
     var disabledAction = (options.onDetectImageIsDisabled && typeof(options.onDetectImageIsDisabled)=='function')?options.onDetectImageIsDisabled:function(){}; 
     var disabledActionRun = function(){ 
      if(actionCounter) return; 
      actionCounter++; 
      disabledAction(); 
     } 
     /* create image */ 
     var img = new Image(); 
     var currentTime = (new Date()).getTime(); 
     if(navigator.appName.indexOf('Microsoft Internet Explorer') != -1){// ie 
      img.onload = enaledActionRun; 
      img.onabort = enaledActionRun; 
      img.onerror = enaledActionRun; 
      img.src = currentTime+'.'+currentTime+'?time='+currentTime; 
      setTimeout(function(){ 
       disabledActionRun(); 
      }, 0); 
     }else if (navigator.appName.indexOf('Opera') != -1) {// opera 
      img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=="+'?time='+(new Date()).getTime(); 
      if(img.complete){ 
       enaledActionRun(); 
      }else{ 
       disabledActionRun(); 
      } 
     }else{// other 
      img.src = currentTime+'.'+currentTime+'?time='+currentTime; 
      if(img.complete){ 
       disabledActionRun(); 
      }else{ 
       enaledActionRun(); 
      } 
     } 
    } 
    // tested in: ff 2+, opera 9+, chrome, safari 4+, ie 6+ 
</script> 

Live demo. 唯一的問題 - 即在最低異步:

setTimeout(function(){ 
disabledActionRun(); 
}, 0); 
+0

哇,這太棒了,我從來沒有想過任何人會真的試圖解決這個問題,你的JS是真棒btw。 –