Problem with getSelection() in iframe in chrome

C

Ciaran

Hi, I'm working on a WYSIWYG editor and I've run into some difficulty
trying to select a range in chrome. I believe the problem is the same
as mentioned here: http://www.google.com/support/forum/p/Chrome/thread?tid=1ca7b96da3e71324&hl=en
but none of the solutions listed seem to work for me - I just keep
getting errors.


Here's my code:
The idea is that it finds the tag with the ID "caret", selects it and
deletes it, leaving the caret (text cursor) in its position. It works
like a charm in Firefox but chrome alerts null as the selection object
rather than [object HTMLBodyElement] like FF. I only need support for
FF & chrome so IE is not an issue (thank god!)

function resetCaret(iframename){
iframe=document.getElementById(iframename).contentWindow
referenceNode = iframe.document.getElementById("caret");
if(referenceNode){
sel=iframe.getSelection()
alert(sel.focusNode)
range=sel.getRangeAt(0);
range.selectNode(referenceNode);
range.deleteContents();
}
}

Thanks for any help - this has been going on a long while now and it'd
be great to get it behind me!
Ciarán
 
T

Tim Down

Hi, I'm working on a WYSIWYG editor and I've run into some difficulty
trying to select a range in chrome. I believe the problem is the same
as mentioned here:http://www.google.com/support/forum/p/Chrome/thread?tid=1ca7b96da3e71...
but none of the solutions listed seem to work for me - I just keep
getting errors.

Here's my code:
The idea is that it finds the tag with the ID "caret", selects it and
deletes it, leaving the caret (text cursor) in its position. It works
like a charm in Firefox but chrome alerts null as the selection object
rather than [object HTMLBodyElement] like FF. I only need support for
FF & chrome so IE is not an issue (thank god!)

function resetCaret(iframename){
        iframe=document.getElementById(iframename).contentWindow
        referenceNode = iframe.document.getElementById("caret");
        if(referenceNode){
                sel=iframe.getSelection()
                alert(sel.focusNode)
                range=sel.getRangeAt(0);
                range.selectNode(referenceNode);
                range.deleteContents();
        }

}

Thanks for any help - this has been going on a long while now and it'd
be great to get it behind me!
Ciarán

Firstly, you should use var to declare all your variables inside a
function, otherwise they effectively become global variables.
Secondly, what exactly are you trying to achieve? You say you're
"trying to select a range in chrome", but to what purpose? The
business with selecting and deleting a dummy element is very likely to
be unnecessary in Chrome, since Ranges and selections are fully
described in terms of nodes and offsets.

Tim
 
C

Ciaran

Hi, I'm working on a WYSIWYG editor and I've run into some difficulty
trying to select a range in chrome. I believe the problem is the same
as mentioned here:http://www.google.com/support/forum/p/Chrome/thread?tid=1ca7b96da3e71...
but none of the solutions listed seem to work for me - I just keep
getting errors.
Here's my code:
The idea is that it finds the tag with the ID "caret", selects it and
deletes it, leaving the caret (text cursor) in its position. It works
like a charm in Firefox but chrome alerts null as the selection object
rather than [object HTMLBodyElement] like FF. I only need support for
FF & chrome so IE is not an issue (thank god!)
function resetCaret(iframename){
        iframe=document.getElementById(iframename).contentWindow
        referenceNode = iframe.document.getElementById("caret");
        if(referenceNode){
                sel=iframe.getSelection()
                alert(sel.focusNode)
                range=sel.getRangeAt(0);
                range.selectNode(referenceNode);
                range.deleteContents();
        }

Thanks for any help - this has been going on a long while now and it'd
be great to get it behind me!
Ciarán

Firstly, you should use var to declare all your variables inside a
function, otherwise they effectively become global variables.
Secondly, what exactly are you trying to achieve? You say you're
"trying to select a range in chrome", but to what purpose? The
business with selecting and deleting a dummy element is very likely to
be unnecessary in Chrome, since Ranges and selections are fully
described in terms of nodes and offsets.

Tim


Hi again Tim,
It's the same problem as I was having in this thread a few weeks ago:
http://groups.google.ie/group/comp....5/4aae596b4f782938?hl=en-GB&q=cronoklee&pli=1
I'm trying to save the caret position through an autosave. I've put in
hours to get to this point and this is one last tiny problem of
getSelection() returning null in chrome. Everything else is working
nicely.

Ciarán
 
T

Thomas 'PointedEars' Lahn

Tim said:
Firstly, you should use var to declare all your variables inside a
function,
ACK

otherwise they effectively become global variables.

They do not.


PointedEars
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
They do not.

Ofcourse they don't.
They merely become properties of the global object, whereas
global variables are ... properties of the global object.
The difference is that variables are properties that can't
be deleted. For most cases that difference is completely
irrelevant. So, effectively, they behave as global variables.

/L
 
T

Thomas 'PointedEars' Lahn

Lasse said:
Thomas 'PointedEars' Lahn said:
They do not.

Ofcourse they don't.
They merely become properties of the global object, [...]

They *might* become (deletable) properties of the global object.


PointedEars
 
C

Ciaran

Ofcourse they don't.
They merely become properties of the global object, [...]

They *might* become (deletable) properties of the global object.

PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee



Thanks guys - very helpful
Ciarán
 
T

Tim Down

Ofcourse they don't.
They merely become properties of the global object, [...]

They *might* become (deletable) properties of the global object.

Thomas, I do know this and was careful not to say that undeclared
variables inside a function are actual global variables. Obviously not
careful enough though. For my own information, how do you think I
should I have made the point I was trying to make in that instance?

Tim
 
T

Tim Down

Ofcourse they don't.
They merely become properties of the global object, [...]

They *might* become (deletable) properties of the global object.

Thomas, I do know this and was careful not to say that undeclared
variables inside a function are actual global variables. Obviously not
careful enough though. For my own information, how do you think I
should I have made the point I was trying to make in that instance?

Tim
 
T

Thomas 'PointedEars' Lahn

Tim said:
Thomas said:
Lasse said:
Tim Down wrote:
Firstly, you should use var to declare all your variables inside a
function,

ACK

otherwise they effectively become global variables.

They do not.

Ofcourse they don't.
They merely become properties of the global object, [...]

They *might* become (deletable) properties of the global object.

JFTR: Assignment to them might as well become the cause of a runtime error,
especially in MSHTML.
Thomas, I do know this and was careful not to say that undeclared
variables inside a function are actual global variables. Obviously not
careful enough though. For my own information, how do you think I
should I have made the point I was trying to make in that instance?

I do not think this is an issue that can be easily, completely, and
correctly wrapped into one sentence. In fact, I find it rather difficult to
state correctly and concisely what does not exist when that description
implies that it does exist to begin with: identifiers are not variables
before being declared so, so I do not think I can subscribe to "use `var' to
declare ... variables", but to say "declare all your (local) identifiers (as
variables)" would also be imprecise (although I have done so before).

Maybe someone eloquent and knowledgable speaking English as first language,
has an idea for something well-understood, concise, and correct. Does
"otherwise you have to deal with some unexpected side-effects" quite cover
it? Depends. Is an explanation even necessary at this point? Depends as
well.


PointedEars
 

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top