Create an Image object to achieve the image of the pre-download, if the picture already exists in the browser cache, call the callback function directly, use the OnLoad event to determine whether the picture is loaded complete
01 |
function LoadImage (URL, callback) { |
02 |
var img = new Image (); Create an Image object to achieve a pre-download of the picture |
05 |
if (img.complete) {//If the picture already exists in the browser cache, call the callback function directly |
07 |
Return Return directly without handling the OnLoad event |
09 |
Img.onload = function () {////callback functions are called asynchronously when the picture is downloaded. |
10 |
Callback.call (IMG);//Replace the callback function with this with an Image object |
01 |
</pre><pre code_snippet_id= "395795" snippet_file_name= "blog_20140617_3_4709132" name= "code" class= "HTML" ><pre name= "code" class= "HTML" ><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" > |
04 |
<meta http-equiv= "Content-type" content= "text/html; Charset=iso-8859-1 "> |
05 |
<title> Determine whether the picture is loaded </title> |
12 |
<script language= "JavaScript" > |
13 |
document.getElementById ("Img2"). onload = function () { |
14 |
Alert ("Picture loading completed"); |