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

  • Home
  • SEARCH
  • 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 9214963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:05:34+00:00 2026-06-18T02:05:34+00:00

I have a problem with jQuery. I wanted to get the real width/height of

  • 0

I have a problem with jQuery.

I wanted to get the real width/height of an img.
Now I used this function to get it:

var imageSize = function(image) {
    var realImageSize = { };

    $("<img/>") // Make in memory copy of image to avoid css issues
    .attr("src", $(image).attr("src"))
    .load(function() {
        realImageSize.width = this.width;   // Note: $(this).width() will not
        realImageSize.height = this.height; // work for in memory images.

        alert(realImageSize['height']); // Valid height
    });

    alert(realImageSize['height']); // Undefined

    return realImageSize;
}

So I’m copying the image in memory, and then I get and set the images real size in the .load method.

Now, the problem is that the var realImageSize variable isn’t available in this scope for some reason. Can anyone tell me why?

  • 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-06-18T02:05:36+00:00Added an answer on June 18, 2026 at 2:05 am

    That’s not a problem of scope, it’s fine, but a problem of synchronicity : you’re reading the value before the load callback has been executed.

    The callback you give as argument to load isn’t executed immediately but only after the image has been loaded.

    The usual solution is to not return a value but provide a callback :

    var fetchImageSize = function(image, callback) {
        $("<img/>") // Make in memory copy of image to avoid css issues
        .load(function() {
            var realImageSize = { };
            realImageSize.width = this.width;   // Note: $(this).width() will not
            realImageSize.height = this.height; // work for in memory images.
            callback(realImageSize);
        }).attr("src", image.src);
    };
    
    fetchImageSize(myImage, function(realImageSize){
        alert(realImageSize['height']); // NOT Undefined
    });
    

    Note that you should always set the src after you set the onload callback. Some browsers, if you have the image in cache, might not call the onload callback if you set it after you set the source.

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

Sidebar

Related Questions

I have a problem with Jquery function getJSON, the action url does not trigger
It seems I have a problem with JQuery Lite Content Slider. I get a
I have a little problem with Jquery getJSON function. my json here { entries:
I have a third party JQuery library that does this: var $this = $(this);
I have problem while using jquery maskedinput with asp.net textbox. I have a check
I have problem to add jQuery to some existing code, please help. We have
I have a problem with jQuery parallax effect for my body's background. Here is
I have a problem with jQuery, I want to change a DIV (not the
I have some problem with Jquery Autocomplete plugin ( http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/ ) I got it
I have a problem with Jquery UI modal dialogs. I have modal dialog (dialogA),

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.