Forum Moderators: open

Message Too Old, No Replies

firefox and style.left

         

musicales

3:45 pm on Nov 14, 2005 (gmt 0)

10+ Year Member



apologies if I'm missing something really obvious, but can anyone explain why, in FireFox, the alert in the following code comes up empty, instead of showing the left position of the image:

<html >
<body onload="javascript:doit()">
<script language=javascript>
function doit()
{
var d=document.getElementById('im').style.left
alert(d)
}
</script>
<img src=image.gif id=im name=im>
</body>
</html>

Fotiman

4:01 pm on Nov 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well, if you haven't styled it, then there's nothing set. That would be my best guess.

musicales

4:12 pm on Nov 14, 2005 (gmt 0)

10+ Year Member



yes, I think that was the answer - I assumed there would be a default value - I added a left style and it worked.
Thanks

ajkimoto

7:11 pm on Nov 14, 2005 (gmt 0)

10+ Year Member



musicales,

Here is a little function that will return the current/computed style of elements. Just pass an object reference and a string containing a style attribute and the function will pop an alert box with the value. This will display the default value of non-styled elements as well as the style applied by internal/external stylesheets.

//this function will get the current/computed style in IE and Moz/FF
function getStyle(obj,cAttribute){
//if IE
if (obj.currentStyle){
var curVal=eval('obj.currentStyle.'+cAttribute)
}else{
//if Mozilla/FF
var curVal=eval('document.defaultView.getComputedStyle(obj, null).'+cAttribute)
}
alert('style attribute '+cAttribute+' = ' + curVal)
}

ajkimoto