By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,836 Members | 2,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,836 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Yep
ma******@everyday.com (Martin Karlsson) wrote in message news:<ae**************************@posting.google. com>...
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.
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.