Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 854635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:57:18+00:00 2026-05-15T07:57:18+00:00

Chrome is wrongly reporting width and height values for images during, or just after,

  • 0

Chrome is wrongly reporting width and height values for images during, or just after, load time. Jquery is used in this code example:

<img id='image01' alt='picture that is 145x134' src='/images/picture.jpg' />

<script>
 var img = $( 'img#image01' )

 img.width() // would return 145 in Firefox and 0 in Chrome.
 img.height() // would return 134 in Firefox and 0 in Chrome.
</script>

If you put the script in a $(function(){}) function, the result is the same. but if you run the code a few seconds after the page has loaded, chrome returns the correct result.

<script>
  function example () {
    var img = $( 'img#image01' );

    img.width() // returns 145 in both Firefox and Chrome.
    img.height() // returns 134 in both Firefox and Chrome.
  }
  window.setTimeout( example, 1000 )
</script>

Also if you specify the width and height values in the img tag, the script seems to work as expected in both Firefox and Chrome.

<img id='image01' src='/images/picture.jpg' width=145 height=134 />

But as you cannot always control the html input, this is not an ideal workaround. Can jQuery be patched with a better workaround for this problem? or will I need to specify the width and height for every image in my code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:57:18+00:00Added an answer on May 15, 2026 at 7:57 am

    Your quoted code doing it inline is getting a reasonable result, as the image may well not be loaded before your code executes. You said that you put it in an onload handler and that also has the same result. Did you really mean an onload handler? Because I cannot replicate that result. Note that the jQuery ready function is not an onload handler, it happens much earlier than that. To ensure images are loaded, use the image’s load event or the window load event. jQuery ready happens a lot earlier than that.

    So this should work:

    $(window).bind('load', function() {
        var img = $("#theimage");
        log("Width: " + img.width());
        log("Height: " + img.height());
    });
    

    …(where log is something that outputs to the page), and this should work:

    $(document).ready(function() {
        $("#theimage").bind('load', function() { // BUT SEE NOTE BELOW
            var img = $(this);
            log("Width: " + img.width());
            log("Height: " + img.height());
        });
    });
    

    …but this will not:

    $(document).ready(function() {
        var img = $("#theimage");
        log("Width: " + img.width());
        log("Height: " + img.height());
    });
    

    …because it happens too soon.

    Edit: I really, really should have said in the above: If you’re looking the image’s load event (rather than the window load event), you need to check to make sure the image hasn’t already been loaded, because its load event may have already fired. That would look something like this:

    $(document).ready(function() {
        var img, imgElement;
    
        // Find the image
        img = $("#theimage");
        imgElement = img[0];
        if (imgElement && imgElement.complete) {
            // Already loaded, call the handler directly
            loadHandler.call(imgElement);
        }
        else {
            // Not loaded yet, hook the event
            img.bind('load', loadHandler);
        }
    
        function loadHandler() {
            var img = $(this);
            log("Width: " + img.width());
            log("Height: " + img.height());
            img.unbind('load', loadHandler);
        }
    });
    

    That will call loadHandler at the earliest opportunity, whether the image load won the race or the ready code won the race.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 542k
  • Answers 542k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use: SELECT p.id as a, p.url as b, t.id as… May 17, 2026 at 3:22 am
  • Editorial Team
    Editorial Team added an answer There are two parts to the problem First Issue You… May 17, 2026 at 3:19 am
  • Editorial Team
    Editorial Team added an answer I thought I'd show the regex approach, too. It doesn't… May 17, 2026 at 3:18 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Google chrome doesn't behave the same as other browsers when encountering this nugget: <?php
After test driving Google Chrome for 30 minutes or so, I like it, even
Have just started using Google Chrome , and noticed in parts of our site,
How does Google Chrome command and control multiple cross platform processes and provide a
While browsing with Chrome, I noticed that it responds extremely fast (in comparison with
When using Google Chrome, I want to debug some JavaScript code. How can I
Given that Chrome and Safari use webkit has anyone yet found anything that renders
I've tested in Chrome, do I need to test in Safari?
I was reading googlebooks on chrome, where they talk about why they decided to
Does anyone know if silverlight plugs into chrome, or when they plan to support

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.