gpt4 book ai didi

javascript - image onerror 没有在 ie7 中注册?

转载 作者:行者123 更新时间:2023-11-29 22:30:41 25 4
gpt4 key购买 nike

所以我有这个脚本:

<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 属性返回“无”。不是ie7的情况。第二个 if 语句尝试处理 ie 的情况,但由于某种原因,.onload 或 .onerror 都没有被触发。

有没有人遇到过这个可以帮忙?或者指出我正确的方向?我已经尝试了很多方法来做到这一点。但我似乎无法让它工作。

最佳答案

这是我的解决方案:

<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.唯一的问题 - ie 中的最小异步:

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

关于javascript - image onerror 没有在 ie7 中注册?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7084052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com