ElementFromPoint() In Gecko

Jun 16, 2003

I recently had need for the method document.elementFromPoint() found in Internet Explorer (http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/elementfrompoint.asp).

My immediate method for implementing this was artificially creating an arbitrary mouse event (decided on mousemove) at the specified coordinates, setting up an event listener on the document, firing the event, record the target, and remove the listener.

However, I apparently didn't pay close enough attention to this (http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html#Events-EventTarget-dispatchEvent):

The target of the event is the EventTarget on which dispatchEvent is called.

However, I had a hunch that Mozilla was merely redirecting the event to the document to be correct, and actually stored somewhere the "correct" target. After dumping the event object, I found a property which must have been recently introduced (Mozilla 1.4 branch apparently) called "explicitOriginalTarget" which is like originalTarget, but apparently never targets anonymous content, and in this case, also differs by referring to the element where the coordinates of the mouse event were.

Anyhoo, here's some code. I've tested it in various Mozilla 1.4 builds successfully (and double checked explicitOriginalTarget in < 1.4 builds to find that it doesn't exist). Firebird 0.6 is also based on 1.4, so it works in that too. By using the browser's box object and mouse events, instead of hacking my own, I don't think there should be any issues with it picking up elements that can't be seen.


// 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.dispatchEvent(event);
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);

event.preventDefault();
event.stopPropagation();
}
Document.prototype.elementFromPoint__target = null;
}


And the reason I prototyped Document instead of adding it to document directly is that any extra documents you may have loaded on the same page inherit those methods. For example, an iframe or something. And it /should/ be usable in other, non-HTML documents as well, such as MathML, SVG, XUL, etc. :)

View 5 Replies


ADVERTISEMENT

Element Below ElementFromPoint(x,y)?

May 6, 2011

I need create a function that queries a link or an input text according to the x,y location using ementFromPoint(x,y).As as result I need to know:- Type.- URL.- Text.- Tittle.I have been trying to do this with elem.innerHTML however I get a lot of information and not what I need.

View 1 Replies View Related

Any Good Examples Of Usage Of Document.elementfrompoint ?

Jul 23, 2005

What is meant by "element"?

When mouse is moved over "element" at x,y, is that "element" the entire
image or something else?

My thought would be to simply retrieve the thumbnail name or other ID and
use that to identify which large image to show.

Any reasons why NOT to use this property?

View 2 Replies View Related

Gecko DOM Window Onclose Event ?

Dec 3, 2005

I tried to use it but i guess i failed... here is a code:

<html>
<head>
<title></title>
<script type="text/javascript">
function closedWin() {
confirm("close ?");
return false; /* which will not allow to close the window */
}
if(window.addEventListener) {
window.addEventListener("close", closedWin, false);
}

window.onclose = closedWin;
</script>
</head>
<body>

</body>
</html>

View 6 Replies View Related

Parsing Variable In Gecko/Mozilla Containing XML

Apr 18, 2006

I'm still getting to grips with Javascript parsing through http://www.w3schools.com/xml/xml_parser.asp. The problem is that I need a Gecko equivalent to the Internet Explorer example at the bottom of the page.

I need to parse XML that is in a variable from a Prototype ajax request.

View 2 Replies View Related

Gecko Find/Highlight Text In Page

Jul 9, 2002

People have complained that window.find() doesn't work, and everyone knows Gecko doesn't support IE's proprietary TextRange object, some have assumed you can't do this in Gecko.

On the contrary though:


Node.prototype.findTextMatches = [];

Node.prototype.findText = function(query, ignoreCase) {
this.findTextMatches.length = 0;
if (ignoreCase)
query = query.toLowerCase();

var tw = this.ownerDocument.createTreeWalker(this, NodeFilter.SHOW_TEXT, { acceptNode: function(node) {return NodeFilter['FILTER_' + (RegExp(query, (ignoreCase ? 'i' : '')).test(node.nodeValue) ? 'ACCEPT' : 'REJECT')] } }, true);
var offsets = [];
offsets[-1] = query.length * -1;
var totalMatches, trueOffsetDiff;
var range = this.ownerDocument.createRange();

while (tw.nextNode()) {
totalMatches = tw.currentNode.nodeValue.split(RegExp(query, (ignoreCase ? 'i' : ''))).length - 1;
for (var i = 0; i < totalMatches; i++) {
trueOffsetDiff = offsets[offsets.length - 1] + query.length;
offsets[offsets.length] = tw.currentNode.nodeValue.substr(trueOffsetDiff)[ignoreCase ? 'toLowerCase' : 'toString']().indexOf(query) + trueOffsetDiff;

range.selectNode(tw.currentNode);
range.setStart(tw.currentNode, offsets[offsets.length - 1]);
range.setEnd(tw.currentNode, range.startOffset + query.length);
this.findTextMatches[this.findTextMatches.length] = range.cloneRange();
}
offsets.length = 0;
}
return (tw.currentNode != this);
}

Node.prototype.highlightText = function() {
if (this.findTextMatches.length > 0) {
with (window.getSelection()) {
removeAllRanges();
addRange(this.findTextMatches.shift());
}
return true;
}
else
return false;
}


To search all text in the body, ignoring case, you'd go like:

document.body.findText('search query', true);

That loads up an array full of ranges selecting the text into document.body.findTextMatches.

To selectively highlight next occurences after executing findText:

document.body.highlightText();
// highlights first occurence

document.body.highlightText();
// removes previous selection, highlights second occurrence

// etc

Just calling that method over and over will successfully highlight instances. It returns true if it highlighted something, false otherwise,

Also, since I prototyped Node, it works not just in HTML documents, but can work in XML documents too.

Calling document.findText in an HTMLDocument may cause interesting results, as it searches through all the nodes, including <head>...

View 8 Replies View Related

JQuery :: Cross-Domain AJAX URL Works In Gecko & Webkit But Not In Opera & IE?

Aug 6, 2010

How does this cross-domain request work in FireFox, Safari & Chrome and not in IE/Opera ?$(document).ready(function()

[Code]...

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved