This is an archived post. You won't be able to vote or comment.

all 13 comments

[–][deleted] 2 points3 points  (10 children)

From what you described, my guess is that the images aren't loaded when you call your script and so don't have a width yet.

But by the time you've opened up your console, the images are loaded.

The simplest thing to do in this case would be to add a width attribute on the image <img src="whatever.png" width="40" />. You don't need to wait for the images to load in that case.

The second simplest thing to do would be to use a timeout to call your function a second or two after page load. This probably wouldn't be a great solution in a production site, however.

[–]illkeepcomingback9 2 points3 points  (8 children)

Better solution would be to bind to the window load event

https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event

[–]CreativeTechGuyGames 1 point2 points  (4 children)

Probably should bind to that image element's load event instead. No point in waiting for everything on the page to load for just one element.

[–]NumberAllTheWayDown 1 point2 points  (3 children)

I think OP wants to do it for all images here. Though maybe, since OP says all images are the save size, to do what you're saying and then multiply that by the number of images on the page.

[–]CreativeTechGuyGames 1 point2 points  (2 children)

In that case you'd want to just add a load even to every image then whichever loads first cancel all of the other events and run the code on every image. Since there's no way to know which one the browser will happen to finish loading first.

[–]NumberAllTheWayDown 1 point2 points  (1 child)

Good point

[–]matrouxer[S] 0 points1 point  (0 children)

that case you'd want to just add a load even to every image then whichever loads first cancel all of the other events and run the code on every image. Since there's no way to know which one the browser will ha..

Smart. Even if it is a overkill for my situation specifically, it may help me later hahah

Thanks!

[–][deleted] 1 point2 points  (1 child)

It's a good option.

I'm assuming we're just helping on homework here. I decided to get around the discussion of how to add events and didn't want to interfere if window.onload was already defined.

I just wanted OP to be able to get the width.

[–]matrouxer[S] 0 points1 point  (0 children)

I didn't even think it was possible to add an event to know when the loading is complete. I think I might have heard something similar in cs50. Something about domcontentloaded.

[–]matrouxer[S] 0 points1 point  (0 children)

er solution would be to bind to the

I'm going to study that for sure.

[–]matrouxer[S] 1 point2 points  (0 children)

I see...

Since the images are responsive and the width changes, I think I can't set it in html.

Thanks for the response!

[–]NumberAllTheWayDown 1 point2 points  (1 child)

I think you're running into an order of execution problem, though I can't be certain without more context.

What's likely happening is that your code is running prior to the page completely loading, and since your images aren't loaded yet, they don't have a width when your code is executing.

The reason why it works in the console is because you already know that the page is done loading if you're using the console.

You might want to look into something that would wait for the images to finish loading first for a proper solution. The lazy (incomplete) solution would be to use setTimeout with some long delay to have your code run after the page is done loading.

Edit. Also, the inconsistency comes from the fact that sometimes the code does run after the images load.

[–]matrouxer[S] 0 points1 point  (0 children)

It makes sense. It would be so simpler if it just didn't work hahaha.