onload事件在< img>标签过早地在Firefox中启动,而不是其他浏览器 [英] onload event in <img> tags firing prematurely in Firefox, not other browsers
问题描述
onload
事件在Firefox和所有基于Mozilla代码库(Tor等)的浏览器中过早地被触发。例如: < img onload =myFunction()src =image.jpg/>
在图片之后调用 myFunction()
在Chrome,Opera,IE和Safari上完全加载(不知道新的Edge浏览器是如何),这是预期和记录的行为。然而,在Firefox及其亲属身上,当浏览器读取<$ c时,该函数立即被称为 $ c>< img> 标记,即 完成图像加载之前 。这是不是记录的行为,并导致应用程序的问题。
这是几年前在这里确认和详细讨论:
https://bugzilla.mozilla.org/show_bug.cgi?id=626613
我找不到解决问题的任何东西,但是我想知道是否有其他人遇到了这个问题,并找到了解决办法。 / p>
我想这可以解决做类似的事情在这里做
image.onload事件和浏览器缓存
这可能成为你正在寻找的解决方案
var img = new Image();
$(img).one('load',function(){
alert(image is loaded);
});
img.src =http://url.image.jpg;
$(img).appendTo('body');
http://jsfiddle.net/ult_combo/GKFxP/5/
我猜你的问题是onload与img标签关联的是window onload方法 https:// developer .mozilla.org / es / docs / Web / API / GlobalEventHandlers / onload
The onload
event in inline HTML is firing prematurely in Firefox and all browsers based on its Mozilla codebase (Tor, etc.) For example:
<img onload="myFunction()" src="image.jpg" />
This calls myFunction()
after the image is fully loaded on Chrome, Opera, IE, and Safari (not sure about the new Edge browser, though), which is the expected and documented behavior.
However, on Firefox and its relatives, the function is called immediately upon the browser reading the <img>
tag, i.e. before the loading of the image is finished. This is not the documented behavior and causes application problems.
This is confirmed and discussed in detail a few years ago here:
https://bugzilla.mozilla.org/show_bug.cgi?id=626613
I can't find anything that solves the problem, though, and I wonder if anyone else has run into this problem and has found the solution.
I guess this could be solved doing something similar than what is done here image.onload event and browser cache
this could be the solution you are looking for
var img = new Image();
$(img).one('load', function() {
alert("image is loaded");
});
img.src = "http://url.image.jpg";
$(img).appendTo('body');
http://jsfiddle.net/ult_combo/GKFxP/5/
What i guess is your problem is that the onload that is associated to the img tag is the window onload method https://developer.mozilla.org/es/docs/Web/API/GlobalEventHandlers/onload
这篇关于onload事件在< img>标签过早地在Firefox中启动,而不是其他浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!