473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
1 8921
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jeff Thies | last post by:
Is there a NS7, Opera7 or Mozilla equivalent for: document.selection.createRange(); Jeff
3
by: Noah | last post by:
I have a text field in a form. I want the user to be able to click a DELETE button and have the character at the cursor position deleted. This would be just as if the user had pressed the Back...
8
by: lawrence | last post by:
I'm a beginner with Javascript and especially cross-browser Javascript. I got this working in IE, but not in Netscape 7. It seems like, in Netscape, every time I click on a button, the focus shifts...
15
by: lawrence | last post by:
Is this the correct way to test for a method before I use it? createRange() is, I believe, an IE only method. function wrapSelectionInTag(selection, tag) { if (document.selection.createRange)...
2
by: melvynm | last post by:
Currently I'm trying to make a plugin for my browser (Maxthon/IE6) which allows me to select any text on the current page and press the plugin button to create a new tab with the selection. This...
1
by: c.anandkumar | last post by:
Hi How do I figure out if a selection in a document is in a particular control? I have a TEXTAREA in which a user can type in some text, select some text and 'apply' tags around it clicking a...
2
by: dennis.sprengers | last post by:
Ik ben bezig met een eigen UBB editor. Als iemand aan het typen is, zorgt CTRL-B voor een \-tag en nogmaals CTRL-B voor een \ tag. Als je eerst een selectie maakt en dan CTRL-B drukt, wordt de...
2
by: vanditnagar | last post by:
Hi, I am facing the difficulty in the selecting a text on the browser . And highlighting the text. I am using the window.getSelection() to get the object and after...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.