【问题标题】:javascript : image is not loading in internet explorerjavascript:图像未在 Internet Explorer 中加载
【发布时间】:2014-03-06 06:40:08
【问题描述】:

我正在尝试使用 javascript 加载一个简单的图像,因为我正在使用以下代码

function loadImage(src) {
    var image = new Image;
    image.onload = function() {
        document.body.appendChild(image);
    };
    image.onerror = function() {
        document.body.appendChild(document.createTextNode('\nError loading as image: ' + this.src));
    };
    image.src = src;
}

loadImage('http://www.linkedin.com/favicon.ico');
loadImage('http://www.facebook.com/favicon.ico');

Fiddle Link

我面临的问题是在 chrome 和 firefox 中工作,但在 Internet Explorer 中无法工作。我不太习惯使用 javascript。

有什么解决办法吗?

【问题讨论】:

  • 我只下载 .ico 文件,它在 Internet Explorer 中为 fb 和 google 工作,只是链接文件未加载:(
  • stackoverflow.com/questions/1439232/…。如果图标用作特定类型的文件,则 IE 不会显示它。你真的不应该在你的网页中使用 .ICO 文件。

标签: javascript jquery html css internet-explorer-9


【解决方案1】:

IE 不会将 ICO 图像加载到浏览器中,除非其 contentType 为 image/x-icon

我可以通过查看 Firefox 中的网络选项卡来确认 LinkedIn 图像的 contentType 是 application/octet-stream,这可以解释为什么 IE 不会显示它,而 Facebook 图像的内容类型是 IE 喜欢的 image/x-icon

除了不请求在其服务器上设置了该类型的图像之外,您无法从浏览器中获取任何信息。我很确定像 LinkedIn 这样的网站会有其他小图片,您可以链接到该网站。

仅供参考,如果您查看其主页的linkedIn 源代码,您会发现一个指向http://s.c.lnkd.licdn.com/scds/common/u/images/logos/favicons/v1/16x16/favicon.ico 的标签在IE 中工作得很好(因为它是image/x-icon)。

【讨论】:

  • check this fiddle jsfiddle.net/iamabhiee/tEhCR/3 facebook 图片在所有浏览器中加载,而linkedin在ie中没有加载
  • @UndercoverDeveloper - 好的,我想我已经在这里总结了这个问题。
【解决方案2】:

还值得注意的是,一些旧版本的 IE(老实说,我不记得 IE8 是否是受影响的版本之一),onload 事件不会触发图像从缓存加载.

function loadImage(src) {
    function doOnLoad() {
        document.body.appendChild(image);  
    }

    var image = new Image;
    image.onload = doOnLoad;
    image.onerror = function() {
        document.body.appendChild(document.createTextNode('\nError loading as image: ' + this.src));
    };
    image.src = src;
    if (image.complete) {
      doOnLoad();
    }
}

【讨论】:

    【解决方案3】:
    call the function on window load :-
    <script  type="text/javascript">
    
    function loadImage(src) {
    var image = new Image;
    image.onload = function() {
        document.body.appendChild(image);
    };
    image.onerror = function() {
        document.body.appendChild(document.createTextNode('\nError loading as image: '  +     this.src));
    };
    image.src = src;
    }
    
    window.onload=loadImage('http://www.linkedin.com/favicon.ico');
    
    </script>
    
     the image is not loading, when you call the method after load all the elements then it will work.
    

    【讨论】:

    • 感谢您的帮助,但仍然出现同样的错误:/
    • 请在清除浏览器历史记录后查看。你用的是哪个版本的IE?
    猜你喜欢
    • 2012-03-11
    • 2021-04-13
    • 1970-01-01
    • 2011-08-09
    • 2011-06-30
    • 1970-01-01
    • 2014-06-16
    • 2012-12-05
    • 1970-01-01
    相关资源
    最近更新 更多