All browsers bound to the img onload event can be executed.

Source: Internet
Author: User

When you need to bind an onload event to the img, you generally think of using the conventional method to bind the event as follows: Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> img onload event binding (incorrect usage) </title>
<Script type = 'text/javascript '>
Window. onload = function (){
Var img = document. getElementById ('imgid ');
Img. onload = function (){
Alert (1 );
};
};
</Script>
</Head>
<Body>

</Body>
</Html>

Now you will find that alert (1) has not been executed. Why? Especially in ie and ff browsers.
When using the jquery plug-in library, you will find that alert does not play out in ie and operabrowsers, but other browsers are playing out. Why ?!
The window. onload event is executed after the dom elements of the page are loaded, which includes the completion of src loading in the img image. Then img. onload will not be executed,
This is because it monitors whether the src of img is loaded completely.
So how do I bind an onload event to an img image? The Code is as follows:Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> img onload event binding (correct usage) </title>
<Script type = 'text/javascript '>
Window. onload = function (){
Var img = document. getElementById ('imgid ');
Var src = img. getAttribute ('src ');
Img. setAttribute ('src ','');
Img. onload = function (){
Alert (1 );
};
Img. setAttribute ('src', src );
};
</Script>
</Head>
<Body>

</Body>
</Html>

In this way, execute alert (1) In each browser ).
That is, after the dom element on the page is loaded, obtain the dom object of img, obtain its src attribute, set its src to ''null, and then listen to the onload event of img, finally, set the src attribute of img.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.