Topic: Object.positon returns = ' '

Why I can not get an object position here:

Code: PHP

function showAlert(objectName){

       

var left;

var top;



   

left = document.getElementById(objectName).style.left;

top = document.getElementById(objectName).style.top;

       

alert('left='+left+'\ntop='+top);

       



}

The result is

left =
top =


I'm calling this function like this onclick="showAlert('logoImage');"
sad


Ps: I wanna do something more, not just show an alert.

Re: Object.positon returns = ' '

This image has a css format.

#'logoImage'{
    position:absolute;
    left:389px; top:15px;
    z-index: 2;
    visibility: visible;
}

Last edited by Zeus.EX (2007-08-10 12:06:40 AM)

Re: Object.positon returns = ' '

I've had this problem as well.  It seems that the YUI developers have figured out how it can be done (semi) reliably... so, I ended up using YUI for advanced GUI work (note, xajax and YUI are meant to compliment each other, they are not meant to compete, per se)

I am sorry I don't have a simple answer; I hope there is one and someone can post it here! big_smile

// Joe

xajax Developer
Connect to me on LinkedIn:
http://www.linkedin.com/in/calledtoconstruct

Re: Object.positon returns = ' '

Maybe you can use the code bellow:
////////////////////////////////////////////
<script  language="Javascript"> 
function getObjPos(theObj) 

     var  v_t = theObj.offsetTop; 
     var  v_l = theObj.offsetLeft; 
     while(theObj=theObj.offsetParent) 
     { 
           v_t += theObj.offsetTop; 
           v_l += theObj.offsetLeft; 
     } 
     alert("top="+v_t+"\nleft="+v_l); 

</script> 

//Two examples to call the function getPos():
<input type=button value="show position" onClick="getObjPos(this);">
<div id="div1" onClick="getObjPos(div1)">thi is a div</div>

////////////////////////////////////////

I have run this code ,it works well~~
good luck~

(ps: I'm sorry that my English is poor. Hope you can understand me.^_^)

Last edited by rznqp (2007-08-13 3:43:16 AM)

Re: Object.positon returns = ' '

rznqp, don't worry about your english. I'm a Brazilian and don't have a pretty english as well big_smile

Thanks anyway, but I'm using the YUI class...
CtC saved my life before you big_smile:D

Re: Object.positon returns = ' '

To: Zeus.EX
thanks for your encourage!
Brazilian is a far far country to me. But I know Ronaldo . He is very famous with the Chinese youth~

Last edited by rznqp (2007-08-13 6:17:32 PM)

Re: Object.positon returns = ' '

This problem you have is a CSS problem, and the work around given by rznqp is just addressing the solution via the DOM, and not the CSS.. To solve the problem I guess you would have to initialise the CSS via the DOM, and then your first script should work.. just my 2cents worth .. smile

Re: Object.positon returns = ' '

follow Adesso

comment css
/*
#'logoImage'{
    position:absolute;
    left:389px; top:15px;
    z-index: 2;
    visibility: visible;
}
*/

use a function

Code: PHP

function init(){

  with(document.getElementById(objectName).style){

     position='absolute';

     left='389px';

     top='15px';

     zIndex=2;

     visibility='visible';

 }

}

append init to body's onload event handler.

@ @
  L______________eon.Z
   |                     |
  / \                   / \