View Full Version : What Can I replace with elementFromPoint for Netscape browser?

06-15-2003, 06:12 AM
Please help out!!!

The following code works for IE, but not for Netscape.

var objCell = document.elementFromPoint( window.event.x, window.event.y );
if ( objCell.tagName != "TD" )

I would like to know what can be used to get object in Netscape

06-15-2003, 05:10 PM
I was actually just working on an efficient emulation of that method, using various methods. My first few hunches didn't end up working, but I'm sure it still can be done with some intelligent code.

If you don't mind brute-forcing the location, brothercake uses something similar in his chess script:

06-15-2003, 05:17 PM
It certainly is brute force what I've done - it basically iterates through each element and says, where is it - then compares the figures looking for a best-fit with the co-ordinates I want. Not efficient, but it works.

06-15-2003, 11:48 PM
I got it working. However, I am apparently using a property that was introduced in the 1.4 branch of Mozilla, which limits this to Mozilla 1.4 builds, Firebird 0.6, and anything else built recently:

// Program: document.elementFromPoint(int clientX, int clientY) in Gecko
// Author: Jason Karl Davis (www.jasonkarldavis.com)
// Date: 15 June 2003
// Purpose: Emulate Internet Explorer's document.elementFromPoint method as described here:
// http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/elementfrompoint.asp
// Requirements: A browser built off of the 1.4 branch of Mozilla (or better)
// Distribution: You may freely distribute and use this script as long as these comments remain intact

if (navigator.product == "Gecko") {
Document.prototype.elementFromPoint = function(x, y) {
this.addEventListener("mousemove", this.elementFromPoint__handler, false);
var event = this.createEvent("MouseEvents");
var box = this.getBoxObjectFor(this.documentElement);
var screenDelta = { x: box.screenX, y: box.screenY };
event.initMouseEvent("mousemove", true, false, this.defaultView, 0,
x + screenDelta.x, y + screenDelta.y, x, y,
false, false, false, false, 0, null);
this.removeEventListener("mousemove", this.elementFromPoint__handler, false);
return this.elementFromPoint__target;
Document.prototype.elementFromPoint__handler = function (event) {
this.elementFromPoint__target = event.explicitOriginalTarget;

// reparent target if it is a text node to emulate IE's behavior
if (this.elementFromPoint__target.nodeType == Node.TEXT_NODE)
this.elementFromPoint__target = this.elementFromPoint__target.parentNode;

// change an HTML target to a BODY target to emulate IE's behavior (if we are in an HTML document)
if (this.elementFromPoint__target.nodeName.toUpperCase() == "HTML" && this.documentElement.nodeName.toUpperCase() == "HTML")
this.elementFromPoint__target = this.getElementsByTagName("BODY").item(0);

Document.prototype.elementFromPoint__target = null;