Skip to content Skip to sidebar Skip to footer

Javascript: How To Find Out When Images Are Turned Off In Browser?

Would like cross-browser solution.

Solution 1:

You may take a look at this post (first hit on google).

Solution 2:

This is my solution:

<scripttype="text/javascript">detectImageEnabledMode({
        onDetectImageIsDisabled:function(){
            alert('disabled');
        },
        onDetectImageIsEnabled:function(){
            alert('enabled');
        }
    });
    functiondetectImageEnabledMode(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 = newImage();
        var currentTime = (newDate()).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);
        }elseif (navigator.appName.indexOf('Opera') != -1) {// opera
            img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=="+'?time='+(newDate()).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. The only problem - the minimum asynchronous in ie:

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

Post a Comment for "Javascript: How To Find Out When Images Are Turned Off In Browser?"