[Project_owners] Selected text retrieval

Davide Carboni dcarboni at crs4.it
Mon Dec 13 19:53:17 EST 2004

Saleem Mohammed wrote:

>Hello all,
>I dont know if my previous message reached the list. So, I am
>reposting the question.
>I am trying to get selected text in a window from sidebar. I am using this
>function but for some reasons it returns null string. Is there anything
>else that needs to be added ? Am I missing something here? 
>function getSelection() {
> var selection = "";
> var focusedWindow = document.commandDispatcher.focusedWindow;
> selection =focusedWindow.__proto__.getSelection.call(focusedWindow).toString();
> alert(selection);
>XUL data:
><button label="Test" oncommand="getSelection();"/>
>Thanks for your time and help.
>Project_owners mailing list
>Project_owners at mozdev.org

try this code....

    var text = window._content.document.getSelection();

    // if nothing selected in the document then perhaps there is a
    // selection contained within a frame of the current document
    if (!text) {
        for (var i = 0; i < _content.frames.length; i++) {
            text = _content.frames[i].document.getSelection();
            if (text) break;
    //alert (text);

Davide Carboni, 
Information and Communication Technologies
CRS4, Center for Advanced Studies, Research and Development in Sardinia
http://www.crs4.it  -  Tel.: +39 070 9250 303  -  Fax: +39 070 9250 216
Citations is a great tool

More information about the Project_owners mailing list