• 0

How to image size inside invisible element?


Question

for example, something like this:

<div style="display:none;">
<span>
<table>
<tr>
<td>
<img id="image1" src="something.png" style="width:50px; height:100px;" alt="some image" />
</td>
</tr>
</span>
</div>
<script type="text/javascript>
var mi=document.getElementById('image1');
var height=mi.offsetHeight;
var width=mi.offsetWidth;
</script>

since the whole div is invisible, the height and width are both 0, so is there anyway to get the numbers 100 and 50 for height and width?

From my understanding, if I know which element has the display:none style, I can change the style to display:block and get the image size and then set it back to display:none, however currently there may be an arbitrary number of elements nested around the image element, and I don't know which of them is set to display:none, how may I get the image size in this case? Thanks.

10 answers to this question

Recommended Posts

  • 0
i guess this will work

document.getelementbyid("image1").width=50;

document.getelementbyid("image1").height=100;

Load these after the image have loaded

Oops, I meant to say "get image size", not "set image size", somehow I mis-deleted the "get" in the topic title :wacko:

  • 0

If the width and height are always defined in a CSS style (inline or from a stylesheet), then you should be able to retrieve them with:

var mi = document.getElementById("image1");
var width = parseInt(mi.style.width);
var height = parseInt(mi.style.height);

  • 0
wellofsouls, this is maybe something that related to what you wanted to accomplish... check this out

http://code.google.com/p/timthumb/

timthumb.php is probably what the doctor order

The thing is that I need to do it client side with Javascript, so no php allowed :(

  • 0
If the width and height are always defined in a CSS style (inline or from a stylesheet), then you should be able to retrieve them with:

var mi = document.getElementById("image1");
var width = parseInt(mi.style.width);
var height = parseInt(mi.style.height);

Unfortunately many of the images do not have size defined in CSS. :(

  • 0

Can I just ask... why you need it if it's display : none; ?

Are you going to show it?

Are you spacing elements around it?

Maybe the visibility : hidden; might be of more use than display : none;

Or script it in and preload the image, or test it in script. You can use height and width instead of offset...

http://stackoverflow.com/questions/799710/...idth-and-height

Edited by lunamonkey
  • 0
Can I just ask... why you need it if it's display : none; ?

Are you going to show it?

Are you spacing elements around it?

Maybe the visibility : hidden; might be of more use than display : none;

Or script it in and preload the image, or test it in script. You can use height and width instead of offset...

http://stackoverflow.com/questions/799710/...idth-and-height

well, I need to make a javascript function that runs at page load and checks every image in a web page and resize it if it's too large or too small. Maybe the images will be invisible at first, but later made visible by some javascript trigger.

And yeah, I also just figured that preloading the images seems to be the only way :

<script type="text/javascript>
var mi=document.getElementById('image1');
var ti = document.createElement('img');
ti.setAttribute('src', mi.getAttribute('src'));
ti.style.width=mi.style.width;
ti.style.height=mi.style.height;
document.body.appendChild(ti);
var height=ti.offsetHeight;
var width=ti.offsetWidth;
document.body.removeChild(ti);
</script>

it's quite a hassle, but seems to work fine so far.

alternatively, guess I can first try to get the style.width and style.height, and if that fails, get the height and width. That way there's no need to append and remove the image in the body, which may not be safe I think...

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Was it too much to ask to show the icon in this article?
    • Frankly, I blame whoever is writing such articles. "A big improvement/update and/or new feature is now available to everyone! Also, use this unofficial tweak tool to enable it because it actually isn't available to you yet officially and might not in fact even be entirely ready or whatever, hence why it is perhaps not enabled for you*. But it's great and you should enable it!" I mean there's nothing wrong with sharing info about some feature you might need to enable via unofficial means, of course. It's just that these articles tend to essentially end up being two news pieces in one, and one of them tends to be a bit misleading. (*Yes, yes, the "it's a controlled rollout!" thing. Not a fan of that one either. The argument, not the actual rollout.)
    • Thank you. Will do. I read in the release notes that editor config might be at play here.
    • Actually, I think even Microsoft doesn't know how to control it
    • OpenAI is making Codex more useful in Chrome and the cloud by Pradeep Viswanathan OpenAI's Codex now has more than 5 million users, up nearly 4x from earlier this year. To further accelerate Codex's growth among developers, OpenAI today announced that it has agreed to acquire Ona, a company that builds secure cloud execution and orchestration technology for developers. Ona will enable developers to run Codex with persistent and controlled cloud infrastructure for long-running agentic workflows. Right now, most Codex execution happens locally on developers' laptops and PCs, and the agents work continuously for hours. Through Ona, OpenAI aims to make Codex agents keep working for days without being tied to a user’s local machine or an active session. This will be an important capability for enterprises that want to deploy AI agents in production while maintaining control over infrastructure, data, security boundaries, credential scope, logging, and review workflows. Like any acquisition, the deal is still subject to customary closing conditions, including regulatory approvals. Until the deal closes, OpenAI and Ona will continue to operate as separate companies. After closing, Ona’s team will join the Codex team to improve developer workflows. Alongside the Ona acquisition announcement, OpenAI today introduced a few Codex updates. Developers can now save Codex rate limit resets and use them later instead of losing them when they are not needed immediately. OpenAI is also adding a referral option where users can invite a friend to Codex and get a saved rate limit reset. OpenAI today also announced a developer mode for browser use in Chrome and the Codex in-app browser. With this mode, Codex can use the Chrome DevTools Protocol to debug web apps, inspect pages, and work more directly with browser-based development workflows. Developers can use this when they want Codex to profile JavaScript, inspect console output and network traffic, examine web page states including the DOM and applied styles, and more.
  • Recent Achievements

    • One Month Later
      Jamswaz earned a badge
      One Month Later
    • Week One Done
      Jamswaz earned a badge
      Week One Done
    • Rookie
      Marzoid went up a rank
      Rookie
    • Community Regular
      coch went up a rank
      Community Regular
    • One Year In
      slackerzz earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      509
    2. 2
      PsYcHoKiLLa
      186
    3. 3
      +Edouard
      157
    4. 4
      Steven P.
      83
    5. 5
      ATLien_0
      75
  • Tell a friend

    Love Neowin? Tell a friend!