我遇到了一个现在仍然难以重现的错误。以下功能:
function getNaturalImgDim(image) {
var src = image.attr("src");
// create a shadow image for ie purposes to load the image and determine
// the dimensions
var offscreenImg = $('<img>').attr("src", src);
var imageWidth = null;
var imageHeight = null;
if(navigator.userAgent.match(/Trident.*rv:11\./)
&& (image[0].mimeType.indexOf('SVG') >= 0 ||
image[0].mimeType.indexOf('scalable vector graphics') >= 0)) {
//this is a special code for IE 11
//the "inmemory" offscreen image doesn't get load by ie11 before it's attached to the DOM. See "appendTo" below
offscreenImg.css({
position : "absolute",
top : "10000px",
left : "10000px"
}).appendTo('body');
imageWidth = offscreenImg.width();
imageHeight = offscreenImg.height();
//remove the dummy image after the dimension is determined
offscreenImg.remove();
}else{
imageWidth = offscreenImg[0].width;
imageHeight = offscreenImg[0].height;
}
offscreenImg = null;
return new Point(imageWidth, imageHeight);
}
正如您所看到的,我正在确定img标记的维度。我在IE中对svg有特殊待遇。这适用于chrome,firefox和(显然有时候)IE。
我发现有时mimeType已设置,有时则不然。使它更容易混淆。如果未在IE中设置mimeType,则转到else情况并读取宽度和高度。所以,这里有时可以确定宽度和高度,有时可以提供0。
我希望有人可以指出奇怪行为发生的方向。