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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:16:50+00:00 2026-06-09T02:16:50+00:00

I am in a situation that needs be solve with this way; need convert

  • 0

I am in a situation that needs be solve with this way; need convert a local variable to a global variable. There is an example returning image’s real width and height which i found these method from this answer..

Need to convert local varialbes pic_real_height and pic_real_width to global variables with returning their true values.

Here is jsFiddle.

CSS :

img { width:0px; height:0px; }​

jQuery :

console.log($('.imgCon img').height());//returns 0

var img = $('.imgCon img')[0]; // Get my img elem
var pic_real_width, pic_real_height;
$('<img/>').attr('src', $(img).attr('src')).load(function() {
        pic_real_width = this.width;   
        pic_real_height = this.height;

        console.log( pic_real_width + 'x' + pic_real_height );
        // -- returns true 570x320 --
});
//problem starts here:
console.log( pic_real_width + 'x' + pic_real_height );
//returns undefined
// need to return this as an global variable 570x320
  • 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-09T02:16:51+00:00Added an answer on June 9, 2026 at 2:16 am

    This line,

    console.log( pic_real_width + 'x' + pic_real_height );

    does not wait for these lines

        pic_real_width = this.width;   
        pic_real_height = this.height;
    
        console.log( pic_real_width + 'x' + pic_real_height );
        // -- returns true 570x320 -- 
    

    to execute, because its asynchronous.

    Thus,
    console.log( pic_real_width + 'x' + pic_real_height ); executes before callback function gets called (i.e. before you set the width and height ).

    Since, you havent defined them already, they show undefined.

    A trivial solution would be,

    $('<img/>').attr('src', $(img).attr('src')).load(function() {
            pic_real_width = this.width;   
            pic_real_height = this.height;
    
            console.log( pic_real_width + 'x' + pic_real_height );
            // -- returns true 570x320 --
            restOfMyProcessing();
    
    }); 
    
    function restOfMyProcessing() {
        console.log( pic_real_width + 'x' + pic_real_height );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm in this situation that i need a list of lists. for instance i
imagine a situation where you have an application that needs to import data from
I have a situation where I have an instanced class that needs to call
I am in kind of situation that I need to start with a tab
Situation: I need to make an imap client (using java mail api) that if,
Here is my situation. I need to create a report that shows each open
I have a situation that I solved in the following way: //cube_potentials is float8
I need to solve a complex situation, where i already have a Canvas element
Situation: I am writing a program to solve for primes. I need to solve
This is the situation: I'm working on a project where I need to be

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.