<HTML>
<Div> <image src = 'xxx'> </div>
</Html>
VaR image = new image ();
Image. src = URL;
$ ('Div '). append (image );
When the image size is different from the image size on the page, the reason is that after the image path is set, an HTTP request is sent to request the image size. There is a delay loading process in the middle.
The solution is as follows:
// Define an object
var imgload = function (URL, callback) {
var IMG = new image ();
IMG. src = URL;
If (IMG. complete) {
callback (IMG. width, IMG. height);
}else {
IMG. onload = function () {
callback (IMG. width, IMG. height);
AA ();
IMG. onload = NULL;
};
// Call method BB is the method called after the image is loaded successfully. Here, the width and height of the image are displayed after the image is loaded successfully.
Imgload (ImagePath, BB );
Function BB (W, h ){
Alert (w); // width
Alert (h); // high
}