Opera 和 Safari 不触发 image.onload
function controllaFoto(target, username) {
var _URL = window.URL || window.webkitURL;
var img = new Image();
img.onload = function () {
if(this.width > 240 || this.height > 240) {
document.getElementById("photoLabel").innerHTML = "Text";
document.getElementById("preview").setAttribute("src", "profile?to=showImage&username="+username);
target.value = "";
} else {
document.getElementById("photoLabel").innerHTML = "";
document.getElementById("preview").setAttribute("src", this.src);
}
};
img.src = _URL.createObjectURL(target.files[0]);
return true;
}
该脚本由 onchange
和 onsubmit
属性调用,在 Firefox 和 Chrome 中运行良好,但在 Opera 和 Safari 中运行不佳。事件 onload
未触发。为什么?
更新 我正在使用 Windows 进行测试。
更新 2 问题在于 _URL
是未定义
。我尝试过
var _URL = window.URL || window.webkitURL || window.mozURL || window.msURL || window.oURL;
但仍然不起作用。
对于 Chrome,其值为 object DOMURL
,对于 Firefox 为 object MozURLProperty
,但对于 Safari 和 Opera 为 undefined
。所以控制台说:
未捕获的异常:TypeError:无法将“_URL”转换为对象
function controllaFoto(target, username) {
var _URL = window.URL || window.webkitURL;
var img = new Image();
img.onload = function () {
if(this.width > 240 || this.height > 240) {
document.getElementById("photoLabel").innerHTML = "Text";
document.getElementById("preview").setAttribute("src", "profile?to=showImage&username="+username);
target.value = "";
} else {
document.getElementById("photoLabel").innerHTML = "";
document.getElementById("preview").setAttribute("src", this.src);
}
};
img.src = _URL.createObjectURL(target.files[0]);
return true;
}
This script is called by the onchange
and onsubmit
attributes and works fine in Firefox and Chrome, but not in Opera and Safari. The event onload
is not triggered. Why?
UPDATE I'm testing it with Windows.
UPDATE 2 The problem is that _URL
is undefined
. I tried with
var _URL = window.URL || window.webkitURL || window.mozURL || window.msURL || window.oURL;
but still not working.
For Chrome its value is object DOMURL
, for Firefox is object MozURLProperty
, but for Safari and Opera is undefined
. So the console says:
Uncaught exception: TypeError: Cannot convert '_URL' to object
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 此 MDN 页面,它看起来不像
Opera 或某些版本的 Safari 支持 createObjectURL
。仅此一点可能就是您的主要问题。您是否查看过错误控制台,看看它是否抱怨缺少createObjectURL
方法。如果该方法不存在,您将永远不会获得对.src
的分配,因此永远不会有成功的加载事件。此外,我不能肯定地说,但这对我来说似乎也是一个潜在的问题。您可能需要将新创建的 Image 对象存储在某处,以便它持续存在。按照上面编写代码的方式,垃圾收集器可以自由地处置 img 对象,因为不再有任何对它的引用。
也许您还没有向我们展示您的所有代码,但是正如您所写的那样,加载您永远不会使用的图像有什么意义?
因此,我建议要么更改代码的工作方式,不要使用您不会使用的图像。或者,将新创建的 img 标签实际存储在某处,以便它持续存在并且不会被垃圾收集。
我还想问一下你怎么知道图片加载成功?如果您没有使用该图像,您如何知道它已成功加载。
According to this MDN page, it doesn't look like
createObjectURL
is supported in Opera or in some versions of Safari. That alone may be your main issue. Have you looked in the error console to see if it's complaining about the lack of thecreateObjectURL
method. If that method doesn't exist, you would never get an assignment to.src
so thus there would never be a successful load event.In addition, I can't say for sure, but this also looks like a potential issue to me. You may need to store the newly create Image object somewhere so it persists. The way your code is written above, the garbage collector may be free to dispose of the img object because there are no longer any references to it.
Perhaps you haven't shown us all of your code, but as you have it written, what's the point of loading an image that you will never use?
So, I'd suggest either change the way your code works to not use an image that you aren't going to use. Or, actual store the newly create img tag somewhere so it persists and won't get garbage collected.
I'd also ask how you know that the image is loading successfully? If you're not using the image, how do you know it's successfully loading.