Email conversation
From | Florian Sauvin |
To | Me |
Subject | finding the dom xpath of the selection |
Date | 15 June 2004 00:45 |
Hi,
First of all, congratulations for your site, which is great, I
especially like the examples online....
I didn't find on your website the answer to my question...
I want to do a standalone script (that I would like to put as a
bookmark) that would be able to know the dom xpath of the current
selection. How can I do it?
For example, let's say I have a web page, whose code is:
<p align="center">This is a sample of some <b>HTML you
might<br>have</b> in your document</p>
If I select the word HTML, I would like the script to find: /p/b
do you see my problem??
thank you...
--
Florian
From | Me |
To | Alex Khost |
Subject | Re: JavaScript XML Parsing |
Date | 15 June 2004 10:34 |
Florian,
With selections, this would be extremely difficult, since a selection may
extend over several tags. For example in:
<p>Hello World</p>
<p>Goodbye Mars</p>
If I were to select the text 'World Goodbye', what element would the
selection be inside?
Anyway, only Internet Explorer actually creates an object (textrange) for
selected text, and even it cannot give a DOM path for that object. So, the
short answer is no, I do not know of any way to find the DOM path for
selected text.
However, it is possible to work out the DOM path for any mouse event
(except selection events). So, I propose a more simple idea. Instead of
checking for the DOM path to selected text (which is impossible, as far as
I know), why not check for the DOM path to the element that is under the
mouse?
I have written the bookmarklet script called 'DOM path' on:
http://www.howtocreate.co.uk/bookmarklets.html
hope you find this useful
Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
From | Florian Sauvin |
To | Me |
Subject | Re: finding the dom xpath of the selection |
Date | 15 June 2004 18:40 |
first of all, thank you for your quick answer
> <p>Hello World</p>
> <p>Goodbye Mars</p>
just as a question, what if we make the asumption that the text selected
will be only in one single element?
if we select, anyhow the text 'world goodbye', isn't it possible to find
the common father to both elements?
In fact, I need to know the DOM path to enable the user to use a
bookmarklet that would send as an argument the URL of the current page,
plus the DOM path... do you think it is doable? How do you think I can do?
Thank you very much once again... this is VERY useful!
--
Florian
From | Me |
To | Alex Khost |
Subject | Re: JavaScript XML Parsing |
Date | 15 June 2004 22:25 |
>> <p>Hello World</p>
>> <p>Goodbye Mars</p>
>
>
> the text selected will be only in one single element?
> if we select 'world goodbye', isn't it possible to find the common father?
yes, that would not be too difficult, but since no browser actually tells
us what the parent of selected text is, we cannot actually do anything at
all, no matter what text is selected
> send as an argument the URL of the current page, plus the DOM path...
instead of using mousemove, use a click event. The user clicks the
bookmarklet, then clicks the text they want
DOM path (you will need to modify the URL and messages)
Hmm, how about this for an idea. They click the bookmarklet, then they
select the text, as soon as they finish selecting the text, (using the
mouseup event), the script is triggered and sends the information. This
would even make it possible to send the text they selected as well. The DOM
path would be for where they let go of the mouse button.
That way, I could tell if it related to the selecting of text by checking
for selected text. This would work in Internet Explorer 5+ Win/Mac,
Mozilla/Netscape6+, Opera 7.5+. For now at least, this technique cannot
work in anything else:
DOM path
From | Florian Sauvin |
To | Me |
Subject | Re: finding the dom xpath of the selection |
Date | 15 June 2004 23:03 |
would the other way of doing it? Only clicking... work in Safari as
well?
the selecting feature is good, but I don't especially need the selected
text itself, and I rather support more platforms....
From | Me |
To | Alex Khost |
Subject | Re: JavaScript XML Parsing |
Date | 16 June 2004 8:49 |
The click version will work in pretty much all the major DOM browsers,
including:
Internet Explorer 5+ Win/Mac, Mozilla/Netscape6+, Opera 7+, Safari/
Konqueror/OmniWeb 4.5+ and ICEbrowser.
Since I doubt that NetGem and OpenTV (both television browsers) can handle
bookmarklets or mouse events, it would probably not work in them. I also
doubt that you would ever have any visitors using those browsers :)
From | Florian Sauvin |
To | Me |
Subject | Re: finding the dom xpath of the selection |
Date | 16 June 2004 20:03 |
Hi,
I tested the script you gave me, wich seems to work fine... though, I
still have another problem.
the xpath doesn't seem to be enough to describe in a unique manner the
content, for example:
<HTML>
<BODY>
<TABLE><TR><TD>foo</TD></TR></TABLE>
<TABLE><TR><TD>bar</TD></TR></TABLE>
</BODY>
</HTML>
if I'm looking at foo, the xpath returned by your script will be:
/HTML/BODY/TABLE/TBODY/TR/TD/
but the xpath of bar is exactly the same...
do you think it is possible to modify the script such that it would
make the xpath to be something like:
/HTML/BODY/TABLE(1)/TR/TD/
meaning that we are talking about the first table, the two tables being
brothers, both sons of body, we have to distinguish them in a way...
sorry to bother you with my problem, but I really don't understand
javascript well enough...
thank you once again for your VERY precious help!
best regards
-Florian
From | Me |
To | Alex Khost |
Subject | Re: JavaScript XML Parsing |
Date | 17 June 2004 10:01 |
:D I had a feeling you would have a problem with that.
This is not so easy to do, since there is no immediate way to find how many
sibling elements are the same tag, but is not impossible. I will step
through the previous siblings until I run out. If that element is the first,
there will be no count (even if there were more than one), but if it is the
second, the count will be 2. So, for your example, foo would give:
/HTML/BODY/TABLE/TBODY/TR/TD/
but bar would give
/HTML/BODY/TABLE(2)/TBODY/TR/TD/
DOM path (you will need to modify the URL and messages)
From | Florian Sauvin |
To | Me |
Subject | Re: finding the dom xpath of the selection |
Date | 17 June 2004 20:45 |
Cool, I tested it this morning, it work wonderfully well!!!
thanks a lot for your help!!!