JavaScript Simple Way to Get Original Size of Page Picture

  • 2021-06-29 09:50:47
  • OfStack

This article shows an example of how JavaScript can simply get the original size of a page picture.Share it for your reference, as follows:

Here the original width and height are obtained from the Image() object

This is a less cumbersome way to get it by directly assigning new an Image () object to it.


var img = new Image();
img.src = $("#target").attr("src");
if(img.complete){
  alert('width:'+img.width+',height'+img.height);
  img = null;
}else{
  img.onload = function(){
    alert('width:'+img.width+',height'+img.height);
    img = null;
  };
}

And don't worry about one more http request for the new Image object. The browser already has a cache after loading the picture. It's OK for your new N objects. Of course, memory will be consumed, so when you run out, img is set to null.

More readers interested in JavaScript-related content can view this site's topics: Summary of JavaScript Switching Effects and Techniques, Summary of JavaScript Finding Algorithmic Techniques, Summary of JavaScript Animation Effects and Techniques, Summary of JavaScript Errors and Debugging Techniques, Summary of JavaScript Data Structure and Algorithmic Techniques.Summary of JavaScript Traversal Algorithms and Techniques and Summary of JavaScript Mathematical Operation Usage

I hope that the description in this paper will be helpful to everyone's JavaScript program design.


Related articles: