1

Suppose I have this image:

<img src="images/01.jpg" border="0" rel="shadow" />

Then with jQuery, I get its width using:

$('[rel="shadow"]').width();

Firefox and IE report correct dimensions of the image eg 140px while Chrome reports 0. How to solve this problem?

Note

I don't want to set explicit width for images eg:

<img src="images/01.jpg" border="0" rel="shadow" width="140" />

So, how to get width in cross-browser way which is not defined in width attribute of elements?

7
  • What version of Chrome? Is the script inside a load event handler or $(document).ready? Commented Mar 13, 2010 at 19:31
  • @Crescent Fresh: Chrome version is: 4.0.295.0 and the script is inside ready. Commented Mar 13, 2010 at 19:32
  • It seems it poses a problem with both Chrome and Safari. Commented Mar 13, 2010 at 19:32
  • @Anthony Forloney: any possible solution to this? Is this a bug in those browsers? Commented Mar 13, 2010 at 19:34
  • 1
    @loviji: exactly, that's why I asked. The answer is trivial if we know which of load or ready is being used. Commented Mar 13, 2010 at 19:39

1 Answer 1

8
$(window).load(
    function() {
        alert($('img').width());
    }
);

This will work: Test case. Basically it will wait until images load before executing the code (when the ready function fires the document has been fully loaded but images haven't).

Sign up to request clarification or add additional context in comments.

2 Comments

@Andreas Bonini: Perfect, load never popped up in my mind, Thanks :)
@Gagan: you're welcome, I'm happy some of my answers from years ago are still useful today :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.