Jump to content

Images in js


MrAdam

Recommended Posts

hiya people, is it possibile in javascirpt to return the width and height of a given image... (not meaning the width="" and height="" attributes), if so... how? Thanks.

Yes, just use .height and .width after the picture object.
<html><head><script>function check(pic){alert("width: "+pic.width+"px");alert("height: "+pic.height+"px");}</script></head><body><img src="http://www.google.co.uk/logos/worldcup06_uk.gif" onclick="check(this)" /></body></html>

Is that what you need?

Link to comment
Share on other sites

Yes, just use .height and .width after the picture object.
<html><head><script>function check(pic){alert("width: "+pic.width+"px");alert("height: "+pic.height+"px");}</script></head><body><img src="http://www.google.co.uk/logos/worldcup06_uk.gif" onclick="check(this)" /></body></html>

Is that what you need?

If the picture is on your server, how does it have to be the picture path? Because if the src is like this: "../Img/1.jpg" , pic.width and pic.heigth will return undefined.
Link to comment
Share on other sites

If the picture is on your server, how does it have to be the picture path? Because if the src is like this: "../Img/1.jpg"  , pic.width and pic.heigth will return undefined.
I'm not sure what you mean :) If you can access a picture by its path eg http://www.mysite.com/images/pic.jpgthen you can access it's dimensions.
Link to comment
Share on other sites

For now, my pc is my server, so should I use: "http://localhost/Img/1.jpg" as my path?
Yes that should work but if you upload your pages images to internet then other people wount see them."../Img/1.jpg" - that path should work if your page is in another folder at the same level as Img folder - like "http://localhost/Pages/mypage.php"It's all about asolute and relative file paths.so if you were on index.php then this should work - "Img/1.jpg"
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...