je*********@hot mail.com wrote:
Hi,
I am using it to store what a users selects or highlights within a html
page. That position plus the length of the highlight will then be
passed to an application to be stored so that when they revisit the
page, what the user selected will still be highlighted. The app needs
the postion on the page, as well as the length of the highlight.
I'm starting to see what you want, however I'm afraid you won't be be
able to do it the way you want, given the way the page is parsed
client-side: basically you manipulate nodes, not text.
What I can suggest is the following: capture the user selection, alter
the tree, store the altered tree in a DB with the username, and send
the altered tree back when requested by the user. The code below should
work in IE/Mozilla (slightly tested only, I'm on my way to holidays).
Given the nature fo the text to be highlighted, then you might consider
sending only a part of the tree, or even adopt a text-based highlight
(is there's no more than 1 occurrence of the text in your page - you
still don't tell the "real" why:-)).
---
<script type="text/javascript">
var SelectionManage r = (function() {
var MARKER_CLASS="m arkerClass";
function getSel(){
var sel=null;
if(
typeof document.select ion!="undefined " &&
document.select ion &&
document.select ion.type=="Text "
){
sel=document.se lection;
} else if(
window.getSelec tion &&
window.getSelec tion().rangeCou nt>0
){
sel=window.getS election();
}
return sel;
}
function createRange(){
var rng=null;
if(document.bod y && document.body.c reateTextRange) {
rng=document.bo dy.createTextRa nge();
} else if(document.cre ateRange) {
rng=document.cr eateRange();
}
return rng;
}
function moveRange(rng, el){
var moved=false;
if(rng.moveToEl ementText){
rng.moveToEleme ntText(el);
moved=true;
} else if(rng.selectNo deContents) {
rng.selectNodeC ontents(el);
moved=true;
}
return moved;
}
function selectRange(rng ){
if(rng.select){
rng.select();
} else if(window.getSe lection) {
var sel=window.getS election();
if(sel && sel.removeAllRa nges && sel.addRange) {
sel.removeAllRa nges();
sel.addRange(rn g);
}
}
}
function createRangeFrom Sel(sel){
var rng=null;
if(sel.createRa nge) {
rng=sel.createR ange();
} else if(sel.getRange At) {
rng=sel.getRang eAt(0);
if(rng.toString ()=="") rng=null;
}
return rng;
}
function markRange(rng){
var marked=false;
if(rng.pasteHTM L){
rng.pasteHTML(
"<span class='"+MARKER _CLASS+"'>"+rng .text+"<\/span>"
);
marked=true;
} else if(rng.extractC ontents){
var span=document.c reateElement("s pan");
span.className= MARKER_CLASS;
span.appendChil d(rng.extractCo ntents());
rng.insertNode( span);
marked=true;
}
return marked;
}
function createSelection FromNode(node){
var rng=createRange ();
if(rng){
if(moveRange(rn g, node)){
selectRange(rng );
}
}
}
document.onmous eup = function(evt){
var sel=getSel(), rng;
if(sel) {
rng=createRange FromSel(sel);
if(rng) {
SelectionManage r.lastSelection =rng;
}
}
}
return {
lastSelection : null,
markLastSelecti on : function() {
return this.lastSelect ion && markRange(this. lastSelection);
},
highlightSelect ion : function() {
if(document.get ElementsByTagNa me){
var span=document.g etElementsByTag Name("span");
for(var ii=span.length; ii--;) {
if(span[ii].className.inde xOf(MARKER_CLAS S)!=-1) {
createSelection FromNode(span[ii]);
break;
}
}
}
}
};
})();
function submitHandler(f rm){
frm.elements['SelectionInfo'].value=
SelectionManage r.markLastSelec tion()?document .body.innerHTML :"";
return true;
}
window.onload = function(evt){
SelectionManage r.highlightSele ction();
}
</script>
<div>Hello, World!</div>
<form action="foo" onsubmit="retur n submitHandler(t his)">
<input type="hidden" name="Selection Info">
<input type="submit" value="Send selection">
</form>
---
HTH,
Yep.