Home  >  Article  >  Web Front-end  >  Three ways to use JavaScript to determine whether an image has been loaded_javascript skills

Three ways to use JavaScript to determine whether an image has been loaded_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:50:171273browse

Sometimes you need to get the size of the image, which needs to be done after the image is loaded. There are three ways to achieve this, which are introduced one by one below.

1. Load event

Copy code The code is as follows:

< !DOCTYPE HTML>



img - load event



loading...






Tested, all browsers show "loaded" indicates that all browsers support the load event of img.

2. readystatechange event
Copy code The code is as follows:

< !DOCTYPE HTML>



img - readystatechange event



loading...






readyState is complete and loaded indicates that the image has been loaded. Tested that IE6-IE10 support this event, other browsers do not support it.

3. Complete attribute of img
Copy code The code is as follows:





img - complete attribute



loading...






Polling continuously monitors the complete attribute of img. If it is true, it indicates that the image has been loaded and stops polling. This attribute is supported by all browsers.
Three ways to use JavaScript to determine whether an image has been loaded_javascript skills
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn