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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:57:47+00:00 2026-06-07T17:57:47+00:00

So the alert gives undefined values for the width and height. I think the

  • 0

So the alert gives undefined values for the width and height. I think the w and h values of the image from the img.onload calculation is not being passed to the values to return, or it may be returning w and h before the onload calculates them:

function getMeta(url){
 var w; var h;
 var img=new Image;
 img.src=url;
 img.onload=function(){w=this.width; h=this.height;};
 return {w:w,h:h}    
}

// "http://snook.ca/files/mootools_83_snookca.png" //1024x678
// "http://shijitht.files.wordpress.com/2010/08/github.png" //128x128

var end = getMeta("http://shijitht.files.wordpress.com/2010/08/github.png");
var w = end.w;
var h = end.h;
alert(w+'width'+h+'height');

How can I have the alert show the correct width and height?

http://jsfiddle.net/YtqXk/

  • 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-07T17:57:48+00:00Added an answer on June 7, 2026 at 5:57 pm

    Get image size with JavaScript

    In order to read the data from an image you’ll need to make sure it’s first loaded. Here’s a callback-based approach and two promise-based solutions:

    Callback

    const getMeta = (url, cb) => {
      const img = new Image();
      img.onload = () => cb(null, img);
      img.onerror = (err) => cb(err);
      img.src = url;
    };
    
    // Use like:
    getMeta("https://i.stack.imgur.com/qCWYU.jpg", (err, img) => {
      console.log(img.naturalWidth, img.naturalHeight);
    });

    Using the load Event listener (Promise):

    const getMeta = (url) =>
      new Promise((resolve, reject) => {
        const img = new Image();
        img.onload = () => resolve(img);
        img.onerror = (err) => reject(err);
        img.src = url;
      });
    
    // Usage example: 
    ;(async() => {
      const img = await getMeta('https://i.stack.imgur.com/qCWYU.jpg');
      console.dir(img.naturalHeight + ' ' + img.naturalWidth);
    })();

    Using HTMLImageElement.decode() (Promise)

    const getMeta = async (url) => {
      const img = new Image();
      img.src = url;
      await img.decode();  
      return img
    };
    
    // Usage example:
    getMeta('https://i.stack.imgur.com/qCWYU.jpg').then(img => {
      console.dir(img.naturalHeight +' '+ img.naturalWidth);
    });
    • MDN Docs: HTMLImageElement
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If the following works: alert( data.query.results.tbody[1].tr[0].td[0].span.img.src ); Why will this not work? var image
How to give alert if the app is already installed in android,if not how
An alert/prompt called from chrome extension background pake is shown off the screen (or
var loc_name = document.forms['create_<?php echo(htmlspecialchars($window_ID)); ?>'].elements['location_name']; alert(loc_name); This just gives me the message 'undefined'
I wish to use the jQuery.post class to return (not alert) the response within
I'm using jQuery $.getJSON(..) to get some json. The passed value shows me undefined
My problem is that this JSON is not being parsed by JavaScript or the
I want to give an alert after person sign up successfully. And then I
how can I give an alert when one div hovers over another div upon
We alert user using this javascript, now i want to add this functionality to

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.