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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:26:16+00:00 2026-05-24T18:26:16+00:00

function find_image_dm(imgsrc){ window.imgwidth = ; var img = new Image(); img.onload = function test()

  • 0
function find_image_dm(imgsrc){
    window.imgwidth = ""; 
    var img = new Image();

    img.onload = function test() {
        var width = this.width;
        var height = this.height;

        if (height > 300) {
            var x = height/300;
            window.imgwidth = Math.ceil(width/x);
        }
    }

    img.src = imgsrc;

    return(window.imgwidth);
}

I want to return window.imgwidth so i can use it else where but if I try to alert it or anything like that it shows up blank. How do I return its value. window.imgwidth is my attempt to create a global variable. I do not need the width of the window

  • 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-24T18:26:17+00:00Added an answer on May 24, 2026 at 6:26 pm

    The problem with your attempt is that the onload callback executes asynchronously. Because of this, you cannot reliably return anything since the onload callback is executed at some future time. Instead of thinking in terms of return values, think about passing a callback function parameter to your find_image_dm function:

    function find_image_dm(imgsrc, callback)
    {
        var img = new Image();
    
        img.onload = function()
        {
            callback(this.width, this.height);
        };
    
        img.src = imgsrc;
    }
    

    Which you’d use like this:

    function onReturnSize(w, h)
    {
        console.log('image width is', w);
        console.log('image height is', h);
    }
    
    find_image_dm('http://placekitten.com/2200/1200', onReturnSize);
    

    Demo: http://jsfiddle.net/mattball/pTKW4/

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

Sidebar

Related Questions

var town_imgs = $('.town_img img'); var container; var imggg town_imgs.hover(function(){ container = $('<div class=town_img_thmb>'
$('.icon1').mouseover(function(){ $(this).find('img').attr('src', 'recursos/botban/maq1.png'); }); Should work right? It's just an arbitrary test to see
I tried following code: <script type=text/javascript> $(document).ready(function(){ $(image).hover( function () { $(this).append($(<span> ***</span>)); },
Code: $(.box-collapse).live(click,function(){ if(updating()) return false; var b = $(this).parent().parent(); b.find(.album-div).stop(true,false).slideToggle(); $.ajax({ url: addNonce($(this).attr(href)), success:function(data){
My script: (function($){ $.fn.megaswitcher = function(settings) { return this.each(function() { var $i = $(this),
Here is my code so far. function img_find() { var imgs = document.getElementsByTagName(img); var
I've looked everywhere for this function and cannot find the header files to make
I have a function that searches a database table via ajax. This is the
A button click fires my function that fetches image data via an AJAX-call: $(#toggle_album).click(function
I have a img with a hover function to reveal an element. Some of

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.