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 7636685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:44:48+00:00 2026-05-31T07:44:48+00:00

I wrote some jQuery code in which I was getting the width of images

  • 0

I wrote some jQuery code in which I was getting the width of images on screen and storing them in a variable. Then I logged that variable in the console to see it’s value. When the page loads for the first time, it displays correct value, however if I press F5, it displays 0 as the width.

Here’s the HTML:

    <body>
        <img src="img1.jpg" alt="image">
        <img src="img1.jpg" alt="image">
        <script type="text/javascript" src="script.js"></script>
    </body>

Here’s the jQuery code (script.js) :

    jQuery(document).ready(function($) {
        var imgs = $('img');
        var width = imgs[0].width;
        console.log(width);
    });

The console displays 600 when the page is loaded for the first time but when I press F5 it displays 0.
Why is it so?

  • 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-31T07:44:49+00:00Added an answer on May 31, 2026 at 7:44 am

    At jQuery(document).ready() time, there is no guarantee that images are loaded yet. Images must be loaded in order to get their width. Images are loaded asynchronously and may finish after jQuery(document).ready() fires.

    To know for sure that an image is loaded, you either need to have an onload handler for that specific image so you can know when it is specifically loaded or you need to only inspect the image widths when all the images in the page are loaded with window.onload or in jQuery $(window).load(fn).

    The simplest thing to do would be to change your code to this:

    jQuery(window).load(function() {
        var imgs = jQuery('img');
        var width = imgs[0].width;
        console.log(width);
    });
    

    Personally, I would recommend that you use jQuery’s .height() and .width() methods like this:

    jQuery(window).load(function() {
        var imgs = jQuery('img');
        var width = imgs.eq(0).width();
        console.log(width);
    });
    

    You can see this one work here: http://jsfiddle.net/jfriend00/D4CyN/

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

Sidebar

Related Questions

I wrote some code with a lot of recursion, that takes quite a bit
I wrote some CSS in my HTML code to create rollover buttons. Then i
I wrote some naive code(in the sense that it's synchronous calls) for a tableview
I wrote code that fails to use the JQuery autocomplete to fire a result
Alright, so I have some jQuery code that will send an AJAX request to
I was doing some stuff with jQuery parseJSON function. This is the code, which
I've been having some issues with a pagination widget that I wrote in jQuery.
I'm new to jQuery and i'm trying to write some code to go through
I'm learning jQuery and am about to write some pages using intensively that library.
I wrote some code recently (ISO/ANSI C), and was surprised at the poor performance

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.