I got this code from somewhere on the internet but I don't quite understand it. Especially the for loop part.
function FindPosition(oElement) {
if( typeof( oElement.offsetParent ) != "undefined" ) {
for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
posX += oElement.offsetLeft;
posY += oElement.offsetTop;
}
return [ posX, posY ];
}
else {
return [ oElement.x, oElement.y ];
}
}
I don't understand particularly the for loop part here:
for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent )
I expected there to be a middle expression or upper bound instead of just oElement
Please can someone explain to me?
oElementin the middle part is truthy while element exists. It traverses up through its parents until it gets to the root element,oElementbecomesnullwhich is falsy.