A JavaScript question

Discussion in 'Software' started by pwillener, Sep 3, 2013.

  1. pwillener

    pwillener MajorGeek

    So programming questions are now to be filed here in the software forum?

    I have a JavaScript, and in there I need to know the dimensions of an image. Here is what I do
    Code:
        testImage = new Image(); 
        testImage.src = 'http://....jpg';
        setTimeout(function()
            {checkImage(testImage);},
            256);
    In the checkImage function I can obtain testImage.eight and testImage.width.

    If I test the height or width immediately after I assign the image URL, both are 0. After that delay of a ΒΌ sec, both values are there.

    My questions:
    1. what makes the properties appear after a short delay?
    2. is there a way to force the image to be loaded and properties set, inline (without calling setTimeout)?
     
  2. pwillener

    pwillener MajorGeek

    Found something much more clever than an arbitrary delay: onload
    Code:
    testImage = new Image(); 
        testImage.src = 'http://....jpg';
        testImage.onload = function()
            {checkImage(testImage);};
    
    It's not "inline", but at the end it does a better job than using setTimeout.

    But, any comments welcome! :cool
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds