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

M

Martin Karlsson

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
 
Y

Yep

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.
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,567
Members
47,202
Latest member
misc.

Latest Threads

Top