Solved

jQuery: Get Actual Width of Image

Posted on 2013-06-24
5
623 Views
Last Modified: 2013-06-24
How can I get the ACTUAL width of an image, not the width of the image as it is displayed?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {
 $('img:eq(0)').attr('src', $('img:eq(0)').attr('src')).load(function () {

   alert('The width of the image as it is displayed  is: ' +  $('img').width() );
   alert('The actual width of the image is: ' +  '???' );
   //  Should be 2272 

 })
});

</script>
<style type="text/css">
html, body, p, img {
width: 100%;
padding: 0;
margin: 0;
}
</style>

</head>
<body>

<p>
 <img src="http://upload.wikimedia.org/wikipedia/commons/7/7b/Pic-du-midi-dossau.jpg" alt="" />
</p>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
5 Comments
 
LVL 9

Expert Comment

by:TvMpt
ID: 39272283
var img = document.getElementById('imageid'); 
//or however you get a handle to the IMG
var width = img.clientWidth;
var height = img.clientHeight;

Open in new window

0
 
LVL 9

Expert Comment

by:TvMpt
ID: 39272297
sorry you want the real size image and not the resized/rendered one

2.272px × 1.704px
0
 
LVL 16

Author Comment

by:hankknight
ID: 39272330
Does anybody else have a way to solve this using jQuery?
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39272408
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 39272466
Great picture Gary - was over in France last week planning to climb this but the weather put a stop to it :(
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now