Solved

How to find the width of an image before it loads.

Posted on 2014-07-25
2
441 Views
Last Modified: 2014-07-25
I need to know the width of an image before it is called in the html source. I know the name of the image and I'm trying to use javascript to discover the width. This returns a vale of 0 for the width.


var img = new Image();
img.src = "http://mysite.com/graphics/myimage.png"
window.confirm(img.width);
0
Comment
Question by:Gary Samuels
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 21

Assisted Solution

by:Randy Poole
Randy Poole earned 250 total points
ID: 40219655
There is no way to know before it is loaded.  You can however catch this info before you add it to your page and do what needs to be done:
img = new Image();
img.onload = function() {
  alert("(" + this.width + "x" + this.height+")");
};
img.src = "http://mysite.com/graphics/myimage.png";

Open in new window

0
 
LVL 13

Accepted Solution

by:
duncanb7 earned 250 total points
ID: 40219721
You can do get the image width before adding it into DOM

Duncan    
<script type="text/javascript">
var getImageSize = function(src) {
    var img = new Image();
    $(img).bind('load', function(e) {
        var height = img.height;
        var width = img.width;
      //  document.write('width:' + width + ', height: ' + height);
	  alert("width="+width);
    });
    img.src = src;
};	
getImageSize('http://localhost/data/php/water.jpg');
</script>

Open in new window

0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Node.js 11 77
JQuery DataTable Time Sort 14 43
Div not showing up 6 31
CSS Selector Chain NOT working for this situation? 3 11
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question