JavaScript

Find absolute position of an element

Posted on

While working on a project, I discovered that I needed to find the absolute coordinates of an element in a page in order to dynamically position another element before showing it.

A quick Google search yielded this gem from quirksmode.org: JavaScript – Find position.

Here’s the condensed code:

/**
 * Determines the absolute position of an element
 *
 * @author Peter-Paul Koch
 *
 * @see http://www.quirksmode.org/js/findpos.html
 *
 * @param object obj The element whose absolute position is to be found.
 * @return array An array with two elements: 0 is the left position, 1 is the top position.
 */
function findPos(obj) {
	var curleft = curtop = 0;

	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop  += obj.offsetTop;
		} while (obj = obj.offsetParent);

		return [curleft,curtop];
	}
}

text/application type is deprecated

Posted on Updated on

This was news to me–RFC4329 explicity declares text/javascript to be obsolete. Instead, application/javascript is to be used. The problem? Not all browsers support it yet.

Here’s the breakdown:

Firefox

  • Firefox 3.5.4 – supported

Internet Explorer

  • IE 8 – NOT supported

Safari

  • Safari 4.0.3 – supported

Chrome

  • Chrome 3.0 – supported