Solved

jQuery: Get Actual Width of Image

Posted on 2013-06-24
5
625 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 43

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JS Plugin Chaining 2 32
Why is this panel heading creating extra space 2 38
Error in JQuery 5 39
ASP.net Fit 3 divs on screen side by side 3 6
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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)

861 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

23 Experts available now in Live!

Get 1:1 Help Now