By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,401 Members | 1,381 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,401 IT Pros & Developers. It's quick & easy.

read image style.top

P: n/a
hello,

I want to read the style.top of an image when I open the page with:
document.getElementById("myimage").style.top

But unfortunately the value has no properties....

Does this happen, since the image isn't loaded already?

thanks
Joerg
Aug 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ASM
joerg pfeffer wrote:
hello,

I want to read the style.top of an image when I open the page with:
document.getElementById("myimage").style.top

But unfortunately the value has no properties....
objcts (div, img, etc) have only placement properties
if they are set in absolute position ...
or if they have been moved (re-stylled) by JS

and, on my idea, the right call would be :
myTop = document.getElementById("myimage").top
(to verify !)
Does this happen, since the image isn't loaded already?


and, of course, the call to myTop
must wait the page is loaded
(image loading itself is of no importance)
--
Stephane Moriaux et son [moins] vieux Mac
Aug 15 '05 #2

P: n/a
joerg pfeffer wrote:
hello,

I want to read the style.top of an image when I open the page with:
document.getElementById("myimage").style.top

But unfortunately the value has no properties....

Does this happen, since the image isn't loaded already?


As Stephanie pointed out, an element's style object will not have values
for properties inherited from CSS rules or general layout.

You need to investigate getComputedStyle and getCurrentStyle, try the
following function but beware that Mozilla et al want standard
hyphenated style names and IE wants them camelCase. In if you only want
'top', then it won't be a problem:

function GetCurrentStyle( el, prop ) {
if ( window.getComputedStyle ) {
// Mozilla et al
return window.getComputedStyle(el, '').getPropertyValue(prop) );
} // IE5+
else if ( el.currentStyle ) {
return el.currentStyle[prop];
} // IE4
else if ( el.style ) {
return el.style[prop];
}
}
There is a thread "RegExp for hyphen to camelCase" that discusses
solutions to the camelCase/hyphen issue:

<URL:http://groups-beta.google.com/group/comp.lang.javascript/browse_frm/thread/6c90313661245bb5/72776c776ce21e48?q=camelcase&rnum=1&hl=en#72776c77 6ce21e48>
--
Rob
Aug 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.