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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:27:08+00:00 2026-05-17T20:27:08+00:00

Sometimes i’m facing problems, trying to get sizes of an HTML element in question.

  • 0

Sometimes i’m facing problems, trying to get sizes of an HTML element in question.

I’m tracing JavaScript code and see, that width and height of this element are returned as 0.

When i do the same piece of code in browser’s console, correct sizes are returned.

Example code, that demonstrates this problem is following:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8" />
    <title>Decorate image with button</title>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript"></script>

    <script type="text/javascript" charset="utf-8">
        jQuery(function($){
            // Decorate image, wrapped to link to movie with "Play" button
            $("a > img").each(function(){
                var img = $(this);
                var a = img.parent();

                var w = img.width();
                var h = img.height();

                a.html(" \
                    <table style='position: absolute;'> \
                        <tr style='background-color:transparent;'> \
                            <td style='width: 100%; background-color: transparent;'> \
                                <img src='http://30ttclan.extra.hu/e107_images/RTEmagicC_play_button.png.png' style='display: block; width: 25%; margin-left:auto; margin-right: auto;'></img> \
                            </td> \
                        </tr> \
                    </table>");
                a.append(img);
                // Make height of table equals to height of image
                a.find("> table").width( w );
                a.find("> table").height( h );
            });
        });
    </script>
</head>
<body>
    <a href="http://movies.apple.com/media/us/mac/ilife/iphoto/2010/features/apple-ilife-iphoto-features_whats_new-us-20100727_r848-9cie.mov">
        <img src="http://kelsocartography.com/blog/wp-content/uploads/2009/01/iphoto09_map.png">
    </a>
</body>
</html>

This code takes an image, wrapped to anchor and decorates it with “Play” button, that must be centered over it.

Table cell is used for centering.

In Firefox it works well, but does’t in Safari 5.

When we trace the code, where “w” and “h” variables getting width and height, we will see, that they have zeros.

When we say from browser’s console

$("a > img").width()

we will get correct sizes of image.

I think, i’m missing something …

I think, i must catch “rendering complete”-like events … But, as far, as i know, they are not presented in DOM.

How can i get to know, when the HTML element is rendered and displayed to be 100 % sure, that i’m dealing with correct sizes ?

  • 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-17T20:27:09+00:00Added an answer on May 17, 2026 at 8:27 pm

    The problem is solved.

    As mentioned in question How to ensure images are loaded inside a JQuery plugin?

    webkit browsers like Chrome often return 0 size for images because they’re loaded in parallel.

    All, what i need to do, is to use “load” event for image.

    $("a > img").load(function(){
        var img = $(this);
        var a = img.parent();
    
        var w = img.width();
        var h = img.height();
    
        a.html(" \
            <table style='position: absolute;'> \
                <tr style='background-color:transparent;'> \
                    <td style='width: 100%; background-color: transparent;'> \
                        <img src='http://30ttclan.extra.hu/e107_images/RTEmagicC_play_button.png.png' style='display: block; width: 25%; margin-left:auto; margin-right: auto;'></img> \
                    </td> \
                </tr> \
            </table>");
        a.append(img);
        // Make height of table equals to height of image
        a.find("> table").width( w );
        a.find("> table").height( h );
    });
    

    Now, i’m getting correct sizes.

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

Sidebar

Related Questions

Sometimes I have to work on code that moves the computer clock forward. In
Sometimes I get Oracle connection problems because I can't figure out which tnsnames.ora file
Sometimes it can be confusing reading code in instance members that refers to other
Sometimes I get compilation problems with my Eclipse projects, they suddenly cant find any
Sometimes a labeled break or continue can make code a lot more readable. OUTERLOOP:
Sometimes I come across code such as this: import matplotlib.pyplot as plt x =
Sometimes I see the term free-threaded to describe a class or a method. It
sometimes Flash Authoring colors my actionscript inappropriately. for example: i have custom variable that
Sometimes when I see a website I like or sites from respected people, I
Sometimes when I'm editing page or control the .designer files stop being updated with

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.