First, the Load event
12345678910111213141516 |
<!DOCTYPE HTML>
<meta charset=
"utf-8"
>
<title>img - load event</title>
<body>
"img1"
src=
"http://pic1.win4000.com/wallpaper/f/51c3bb99a21ea.jpg"
>
<p id=
"p1"
>loading...</p>
<script type=
"text/javascript"
>
img1.onload =
function
() {
p1.innerHTML =
‘loaded‘
}
</script>
</body>
|
Test, all browsers show "loaded", indicating that all browsers support IMG's Load event.
Ii. ReadyStateChange Events
123456789101112131415161718 |
<!DOCTYPE HTML>
<meta charset=
"utf-8"
>
<title>img - readystatechange event</title>
<body>
"img1"
src=
"http://pic1.win4000.com/wallpaper/f/51c3bb99a21ea.jpg"
>
<p id=
"p1"
>loading...</p>
<script type=
"text/javascript"
>
img1.onreadystatechange =
function
() {
if
(img1.readyState==
"complete"
||img1.readyState==
"loaded"
){
p1.innerHTML =
‘readystatechange:loaded‘
}
}
</script>
</body>
|
ReadyState for complete and loaded indicates that the picture has been loaded. The test Ie6-ie10 supports this event and is not supported by other browsers.
Third, the Complete property of img
123456789101112131415161718192021222324 |
<!DOCTYPE HTML>
<meta charset=
"utf-8"
>
<title>img - complete attribute</title>
<body>
"img1"
src=
"http://pic1.win4000.com/wallpaper/f/51c3bb99a21ea.jpg"
>
<p id=
"p1"
>loading...</p>
<script type=
"text/javascript"
>
function
imgLoad(img, callback) {
var
timer = setInterval(
function
() {
if
(img.complete) {
callback(img)
clearInterval(timer)
}
}, 50)
}
imgLoad(img1,
function
() {
p1.innerHTML(
‘加载完毕‘
)
})
</script>
</body>
|
Polling continuously monitors the complete property of IMG and, if true, indicates that the picture has been loaded and stopped polling. This property is supported by all browsers.
<!DOCTYPE HTML>
<meta charset=
"utf-8"
>
<title>img - complete attribute</title>
<body>
"img1"
src=
"http://pic1.win4000.com/wallpaper/f/51c3bb99a21ea.jpg"
>
<p id=
"p1"
>loading...</p>
<script type=
"text/javascript"
>
function
imgLoad(img, callback) {
var
timer = setInterval(
function
() {
if
(img.complete) {
callback(img)
clearInterval(timer)
}
}, 50)
}
imgLoad(img1,
function
() {
p1.innerHTML(
‘加载完毕‘
)
})
</script>
</body>
Three ways JavaScript determines whether a picture is loaded (go)