If a user is reading my page, and use his mouse selecting some text, I
want to know exactly what text he/she selects, and put these data into
a database... So later, when the user is on the same page, I can tell
him/her what he/she had been interested in...
So this is not just a Javascript thing, can not be solved WITHIN
javascript... I just can't figure out a way.
Anyone can give a hint, great thanks!!!
window.getSelection().toString() will get you the text currently
selected. See
http://developer.mozilla.org/en/docs/DOM:window.getSelection . As for
getting it into a database (I'm assuming you mean on some server), there
are a few possibilities; one is using an iframe to do a form submission,
another is using XMLHTTPRequest to send the data.
No, It CAN'T.
For example, you have multiple "Click Here" and the user selected one
of them, how do you know which one the user selected?
Another thing, how do you "Re-Locate" the exact positon the selection
LATER on ANOTHER computer?
btw: it's relatively easy to post data to a DB, so that's not the focus
point.
The rest of the functions in the object returned by
window.getSelection() will get you the nodes in the document that were
selected.
http://developer.mozilla.org/en/docs/DOM:Selection
Neil
1. I need to save the user's selection (including the position in page,
and the selected text) to a database in a remote server.
2. I need to "re-create" the selection when the user view the same page
sometime later on another computer.
I know the object returned by "window.getSelection()" contains the
information I need, but how could I save these information (which is a
Javascript object) to a database (like MySQL, that can only work with
numbers & strings) and then use these data to highlight the text on the
same page on ANOTHER computer?
http://developer.mozilla.org/en/docs/JSON
-Boris
There must be a simpler way to do this;
It's just a way to remember the location of a selection - without so
much muss & fuss.
Well. The guy I was replying to wanted to save all the information in a
Selection object and then recreate it later. Given that the Selection object
might get more properties added to it, JSON is the way to go.
If all you want is to save the selection location, you have to define what you
mean by "the selection location" first. For example, how resistant does it have
to be to document mutations? Does it need to be applicable to other documents
parsed from the same source? And so forth.
-Boris
Yes, all I want is to save the selection location, and then, in some
other computer, when someone else is surfing the same page, I can re-
create the selection by finding the location and use some "pasteHTML"
trick to select it again.
Any idia? Thanks!
In that case you basically want to save the information needed to recreate the
selection range. So some sort of fixPtr or something to the range anchor points.
-Boris
Using Boris' suggestion this test document
<http://home.arcor.de/martin.honnen/javascript/storingSelection1.html>
stores the current selection as a cookie in the form of
XPathToStartContainer|startOffset|XPathToEndContainer|endOffset
and then restores it when the page is being reloaded.
That seems to work fine (with Mozilla and Opera 9) but is not thoroughly
tested. It also only works with HTML documents as XPath generation with
mixed namespace documents is more difficult.
If you are writing an extension then obviously you don't want to store
the selection in a cookie but send it (e.g. as JSON
{ 'startXPath': makeXPath(range.startContainer),
'startOffset': range.startOffset,
'endXPath': makeXPath(range.endContainer),
'endOffset': range.endOffset
}
) to a server and then send it back.
The code also does not check whether the stored range still exists, if
not it simply gives an error when the attempt is made to set the range.
--
Martin Honnen
http://JavaScript.FAQTs.com/