문제

The question may look simple, but I have found the answer to be very elusive. I have a shoutcast server broadcasting two images, and they can only be retrieved from certain urls, which don't contain the file's type. What I'm trying to do is get the album art that is being broadcast on a constant url. If that url doesn't have an image that can used, switch to the other url. The code I have written is on JS fiddle. And here's the trouble spot:

try { console.log("testing..."); if (img2.height > 0) { //I have to use something other than the image height, because //that's already determined by CSS //thus always setting clear to true, //regardless of the image. console.log("cleared [true]"); clear = true; } else { console.log("cleared [false]"); clear = false; }

One solution I thought might work is the user's browser throws up an error in the console "Resource interpreted as Image but transferred with MIME type text/plain:" but from what I gathered, javascript can't access that kind of information. I've also tried numerous comparisons in the if statement, but with no luck, they always give the same results, regardless of the image. So I've hit a roadblock and don't really know what I need, I hate leaving this so open-ended, but it's really the best I've got. I've got the server running two music tracks and their album art, I'll let you guys play around with it.

도움이 되었습니까?

해결책

Use the onerror event, like:

img2.onerror = function(){
  alert('see');
}

http://jsfiddle.net/364F8/1/

I'm not rewriting your code, but you should 'see' how it works with the example.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top