views:

212

answers:

2

I am able to highlight the text on the HTML page(rendered through gtkmozembed), which is selected, like below.

    var range, sel;
    if (window.getSelection) {
        sel = window.getSelection();
        if (sel.getRangeAt) {
          range = sel.getRangeAt(0);
        }
        document.designMode = "on";
        if (range) {
            sel.removeAllRanges();
            sel.addRange(range);
        }
        document.execCommand("HiliteColor", false, colour);
        document.designMode = "off";
   }  

Well,it works very fine.Now i am trying to store the information(startNode, startOffset,endNode, endOffset) about the highlighted text, and next time when i open the same page,highlight the same text. I am able to successfully store the info and retrieve them when the same page opens. And i am trying to highlight the text using following code.

    var range = document.createRange();
    range.setStart(startNode, startOffset);
    range.setEnd(endNode, endOffset);

    document.designMode = "on";
    range.execCommand("HiliteColor", false, colour);
    document.designMode = "off";

But it is not working as i am expecting. Can anyone help me to achieve the required? Thanks...

A: 

This page should give you all the details about highlighting via script. I haven't done it myself, so it's probably best you use the page's recommendations.

Delan Azabani
+1  A: 

The execCommand method is a method of the document, not the Range. Also, hilitecolor only works in Firefox, so you should fall back to using backcolor in WebKit and Opera.

EDIT

Added code to save and restore selection

// Assuming a Range object stored in a variable called 'range'

// Save current selected range
var sel = window.getSelection();
var selectedRange = sel.getRangeAt(0);

// Select desired range
sel.removeAllRanges();
sel.addRange(range);

document.designMode = "on";

// Try HiliteColor first since Firefox applies BackColor to the whole block
if ( !document.execCommand("HiliteColor", false, colour) ) {
    document.execCommand("BackColor", false, colour);
}

document.designMode = "off";

// Restore previously selected range
sel.removeAllRanges();
sel.addRange(selectedRange);
Tim Down
Hi, thank you for answering... still it is not working... this code is working fine when i try to highlight the text which is currently selected.i.e when i create the range like this. sel = window.getSelection(); range = sel.getRangeAt(0);It highlights the text which falls into range() element. But if i create the range using setStart() and setEnd(), it is not working...even though execCommand is of document's, somehow i need to specify, it should highlight the text under the range element, right? how can i do that? thank you....
ganapati
This method will only work if the range is selected, since the commands executed by `document.execCommand` are designed to work on the current selection. I think your best option is to temporarily select the range you want to highlight. I will update my answer.
Tim Down
Hi,thanks it works,with small modification. "document.designMode = "on";" This function should be called before calling,"sel.addRange(range);".Thanks once again...
ganapati