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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:36:21+00:00 2026-05-23T14:36:21+00:00

I am having an odd problem with jQuery 1.5.2 in Chrome. I have code

  • 0

I am having an odd problem with jQuery 1.5.2 in Chrome.

I have code that looks like the following

$("img").each(function () {
    if (this.complete) {
        imageOnload.call(this);
    } else {
        $(this).bind("load", imageOnload);
    }
});

function imageOnload () {
  var $this = $(this);

  foo($this); 
}

function foo ($img) {
  var width = $img.innerWidth();
  var height = $img.innerHeight();
}

Keel in mind that this is really simplified for the question.

The above works great in most cases. The problem is that when $img(":visible") == false, Chrome is returning 0 for innerWidth and innerHeight. I actually do need to take padding into account, hence the inner functions.

What is the best way to go about handling this situation? I am currently using

function foo ($img) {
    var width;
    var height;

    if ($img.is(":visible")) {
        width = $img.innerWidth();
        height = $img.innerHeight();
    } else {
        var img = $img[0];
        var pt = parseInt(img.style.paddingTop);
        var pb = parseInt(img.style.paddingBottom);
        var pl = parseInt(img.style.paddingLeft);
        var pr = parseInt(img.style.paddingRight);

        width = img.width + pl + pr;
        height = img.height + pt + pb;
    }
}

but I know this is rife with problems. I can’t quite unravel how jQuery calculates individual padding, but I suspect adapting that would improve the else section of my hack.

Is there a better solution or workaround to getting innerWidth and innerHeight when $img(":visible") == false?

Thanks.

ADDENDUM:

As noted in a comment below, the solution is not as simple as making the image visible, calculating, and then hiding. If something in the parent chain is not visible, then the problem happens. In addition, a few things can trigger the problem.

Height and width calculate to zero in both Chrome and FireFox when the image or a descendent has display:none. Chrome, but apparently not FireFox, also does when the image is masked off because a descendent has overflow:hidden. I am going to try to see how this works in Safari later this weekend.

  • 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-23T14:36:22+00:00Added an answer on May 23, 2026 at 2:36 pm

    Unless someone comes up with a better explanation of what is going on, or a better solution, I think this is the best way to handle the situation:

    function foo ($img) {
        var width;
        var height;
    
        if ($img.is(":visible")) {
            width = $img.innerWidth();
            height = $img.innerHeight();
        } else {
            var img = $img[0];
    
            width =  img.width;
            height = img.height;
    
            var style = null;
    
            if (window.getComputedStyle) {
                style = window.getComputedStyle(img, null);
            } else if (img.currentStyle) {
                style = img.currentStyle;
            }
    
            if (style) {
                var pt = parseInt(style.paddingTop);
                var pb = parseInt(style.paddingBottom);
                var pl = parseInt(style.paddingLeft);
                var pr = parseInt(style.paddingRight);
    
                width +=  img.width + pl + pr;
                height += img.height + pt + pb;
            }
        }
    }
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.