473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

[jQuery] Why does img_width always return 0 (zero)?

I'm rewriting my own js library to jquery functions. A huge undertaking,
but hopefully worth it in the future :) Question: why does the script
always return 0 for img_width? Logo.gif exists and moving the code to
the end of the page (before the closing body-tag) doesn't help either.
Any help would be greatly appreciated!

<html>
<head>
<script type="text/javascript" src="jquery-1.2.6.js"></script>
<script>
$(document).ready(function(){
$("img.caption").each(function(i) {
var img_width = $(this).width();
var img_title = $(this).attr('title');
var img_align = $(this).attr('align');
$(this).wrap("<div class=\"image-caption-container\"
style=\"float:" + img_align + "\"></div>");
$(this).parent().width(img_width);
$(this).parent().append("<div class=\"image-caption\">" +
img_title + "</div>");
});
});
</script>
</head>
<body>
<img src="logo.gif" class="caption" title="scripting like a rockstar">
</body>
</html>
Nov 15 '08 #1
3 5444
Reveller wrote:
Question: why does the script always return 0 for img_width? Logo.gif
exists and moving the code to the end of the page (before the closing
body-tag) doesn't help either.
The ready() function is firing before the image has loaded. It's not
possible to know the image dimensions until the image has loaded. I tested
your code, and the first time through, I get an invalid width of 24 in
Firefox and 28 in IE (probably the default widths the browsers use until the
images have been loaded). Running the page again, because the image is
cached from the first time, the image is already loaded when the code runs.
I then get a correct img_width (460 pixels in my case).

Nov 15 '08 #2
SAM
Le 11/15/08 3:06 PM, Reveller a écrit :
I'm rewriting my own js library to jquery functions. A huge undertaking,
but hopefully worth it in the future :) Question: why does the script
always return 0 for img_width? Logo.gif exists and moving the code to
the end of the page (before the closing body-tag) doesn't help either.
Any help would be greatly appreciated!
and what gives the button :

<button onclick="alert('img width = '+document.images[0].width);">
width ? </button>
Strange ... this code works on reload :-(
It's like the image seems to be put in cache and jquery lost it,
getting it back when refreshing the file.

What $(document).ready is supposed to mean ?

<html>
<head>
<script type="text/javascript" src="jquery-1.2.6.js"></script>
<script>
$(document).ready(function(){
$("img.caption").each(function(i) {
var img_width = $(this).width();
var img_title = $(this).attr('title');
var img_align = $(this).attr('align');
$(this).wrap("<div class=\"image-caption-container\"
style=\"float:" + img_align + "\"></div>");
$(this).parent().width(img_width);
$(this).parent().append("<div class=\"image-caption\">" +
img_title + "</div>");
});
});
</script>
</head>
<body>
<img src="logo.gif" class="caption" title="scripting like a rockstar">
</body>
</html>
Nov 15 '08 #3
Ben Amada wrote:
The ready() function is firing before the image has loaded. It's not
possible to know the image dimensions until the image has loaded.
BTW, your code seems to work if you run the code when the document has
loaded via the body's onload event, rather than using jQuery's document
ready() function.

Nov 16 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ziobudda | last post by:
Hi, I want ask you if, for a web portal/application, is better prototype or Jquery? I don't want to innesc some type of flame, but after the announce that drupal use JQuery and that the new...
1
by: DuaneMoraes | last post by:
A new book on jQuery, the powerful JavaScript library, has been announced by Packt In Learning jQuery, Karl Swedberg and Jonathan Chaffer, creators of the popular jQuery learning resource...
1
by: mikeh3275 | last post by:
I'm new to developing in jQuery, so I'm probably doing this wrong. I'm loading a form into a modal dialog box and I'm trying to assign a click event to the button in the form, but I can't seem to...
83
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about...
6
by: lorlarz | last post by:
Although I believe your criticisms of jQuery are without merit, I have tried to see the fuss in a positive light. I, thusly, have decided that perhaps there is a need for YET further transparency...
20
by: Aaron Gray | last post by:
There does not seem too be anyway to test if two jQuery references are the same element. Given :- ... <div id="1"></div .... Then :- alert( $("#1") == $("#1"))
26
by: RobG | last post by:
Do some of the regulars here need to re-think their (sometimes strident) opposition to libraries? Both Microsoft and Nokia have announced support for jQuery. It seems to have gained quite a bit...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.