Is it possible to restore a selection created with createRange()?

Discussion in 'Javascript' started by Martin Karlsson, Jul 24, 2003.

  1. Hi guys,

    Does anyone know how I can restore a selection created with
    createRange()?

    The problem is that I have a function that opens a new window with
    window.open() which of course makes the current window to lose focus
    and drop the selection. The same happens if I display an alert
    message.

    What I want to do is to somehow restore the selection when the new
    window is closed. I don't know if this is possible, but I would really
    appreciate any tips or advices in this matter.

    Thanks, Martin
    Martin Karlsson, Jul 24, 2003
    #1
    1. Advertising

  2. Martin Karlsson

    Yep Guest

    (Martin Karlsson) wrote in message news:<>...

    > Does anyone know how I can restore a selection created with
    > createRange()?


    A range is an object like any other, just save it and put it into the
    selection when you need it, this should work provided the underlying
    DOM remains unchanged. Following code tested IE5.5 and Mozilla 1.3.


    <script type="text/javascript">
    function GSel(){
    var d=document;
    if(d.selection)
    return d.selection.type=="Text" ? d.selection : null;
    if(window.getSelection)
    return window.getSelection();
    return null;
    }

    function CRng(){
    var sel=GSel();
    if(sel){
    if(sel.createRange) return sel.createRange();
    if(sel.rangeCount && sel.getRangeAt) return sel.getRangeAt(0);
    }
    return null;
    }

    function Sel(rng){
    if(rng.select) rng.select();
    else {
    var s=GSel();
    if(s.removeAllRanges && s.addRange){
    s.removeAllRanges();
    s.addRange(rng);
    }
    }
    }

    function r(){ if(RNG) Sel(RNG); }
    function s(){ RNG = CRng(); }

    var RNG=null;
    </script>

    Select some text here.
    <input type="button" value="Save selection" onclick="s()">
    <input type="button" value="Restore the selection" onclick="r()">


    > What I want to do is to somehow restore the selection when the new
    > window is closed. I don't know if this is possible, but I would really
    > appreciate any tips or advices in this matter.


    Save the range while opening the window, and restore it by calling the
    "r" function in the onunload handler of the child window.
    Yep, Jul 24, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWljaGFlbCBTTA==?=

    createRange / createTextRange ?

    =?Utf-8?B?TWljaGFlbCBTTA==?=, Feb 16, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    2,138
    =?Utf-8?B?TWljaGFlbCBTTA==?=
    Feb 16, 2004
  2. lawrence
    Replies:
    7
    Views:
    141
    Thomas 'PointedEars' Lahn
    May 19, 2004
  3. lawrence
    Replies:
    14
    Views:
    216
  4. Replies:
    2
    Views:
    254
  5. Martin Nadoll

    document.selection.createRange()

    Martin Nadoll, Apr 20, 2009, in forum: Javascript
    Replies:
    1
    Views:
    355
    Thomas 'PointedEars' Lahn
    Apr 20, 2009
Loading...

Share This Page