开发者

How to use new Image() in Internet explorer 8?

开发者 https://www.devze.com 2023-04-01 07:24 出处:网络
I\'ve an unpleasant issue with Internet Explorer (of course). The thing is that this doesn\'t refresh the image in Internet Explorer 8 while in other browsers it does perfectly. I believe that the iss

I've an unpleasant issue with Internet Explorer (of course). The thing is that this doesn't refresh the image in Internet Explorer 8 while in other browsers it does perfectly. I believe that the issue lies in this part o开发者_运维百科f the code, but I can't figure what is it.

var img = new Image();
$(img).load(function() {
    monitor.holder.empty()
    .append(this)
    .fadeIn('fast');
}).addClass('round').attr({'src': 'ylemiste/'+ json.filename, 'alt': json.filename, 'width': '640', 'height': '480'});


Alright, I figured it out. The issue is with ajax call getting cached:

$.ajax({ url: "last.php", dataType: "json", cache: false}).success(function(json){
    ...
});

Adding: cache: false did the trick.


I had this exact issue and after hours of playing with different options, this was the ultimate fix:

Orig Code Structure:

Note: This worked in all browsers, excluding the one developed by our friends at Microsoft.

var img = new Image();
var url = 'http://i.imgur.com/DQP7F.jpg';

$( img )
  /*wait for image to load*/
  .load( function () {

    alert( 'All loaded' );

  })

  /*problem getting img?*/
  .error( function() {

    alert( 'Bad things' );   

  })

  /*set img src*/
  .attr( 'src' , url );

.

With The Fix:

var img = new Image();
var url = 'http://i.imgur.com/DQP7F.jpg';

$( img )
  /*wait for image to load*/
  .load( function () {

    alert( 'I fire after the image has fully loaded' )

  })

  /*problem getting img?*/
  .error( function() {

    alert( 'Bad things' );   

  })

  /*set img src*/
  .attr( 'src' , url );

  /*IE hack; really hate you right now IE*/
  if( $.browser.msie && parseInt( $.browser.version ) < 10 ){

    img.src = url;

  }

You may ask "Why parseInt( $.browser.version ) < 10 rather than 9?"... well, I just don't trust IE, period. I'd rather just give IE9 the fix as well rather than deal with any IE related issues.

I hope this helps someone.

0

精彩评论

暂无评论...
验证码 换一张
取 消