Home  >  Article  >  Web Front-end  >  How does JavaScript determine whether an image has been loaded in order to obtain its size_javascript skills

How does JavaScript determine whether an image has been loaded in order to obtain its size_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:49:201157browse

Sometimes it is necessary to obtain the size of an image. This needs to be done after the image is loaded. How can I do this?

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.
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