Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Problem with getSelection() in iframe in chrome (http://www.velocityreviews.com/forums/t939764-problem-with-getselection-in-iframe-in-chrome.html)

Ciaran 10-30-2009 03:55 AM

Problem with getSelection() in iframe in chrome
 
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/...a3e71324&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).content Window
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

Tim Down 10-30-2009 11:54 AM

Re: Problem with getSelection() in iframe in chrome
 
On Oct 30, 3:55*am, Ciaran <cronok...@hotmail.com> wrote:
> 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/...=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).content Window
> * * * * 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

Ciaran 10-30-2009 01:26 PM

Re: Problem with getSelection() in iframe in chrome
 
On 30 Oct, 11:54, Tim Down <timd...@gmail.com> wrote:
> On Oct 30, 3:55*am, Ciaran <cronok...@hotmail.com> wrote:
>
>
>
>
>
> > 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/...=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).content Window
> > * * * * 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.l...ronoklee&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



Thomas 'PointedEars' Lahn 10-30-2009 10:46 PM

Re: Problem with getSelection() in iframe in chrome
 
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.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>

Lasse Reichstein Nielsen 10-30-2009 10:51 PM

Re: Problem with getSelection() in iframe in chrome
 
Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:

> 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, 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
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'


Thomas 'PointedEars' Lahn 10-31-2009 11:29 AM

Re: Problem with getSelection() in iframe in chrome
 
Lasse Reichstein Nielsen wrote:

> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>> 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.


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

Ciaran 11-01-2009 07:50 PM

Re: Problem with getSelection() in iframe in chrome
 
On 31 Oct, 11:29, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> Lasse Reichstein Nielsen wrote:
> > Thomas 'PointedEars' Lahn <PointedE...@web.de> writes:
> >> 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.
>
> 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

Tim Down 11-02-2009 12:40 AM

Re: Problem with getSelection() in iframe in chrome
 
On 31 Oct, 11:29, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> Lasse Reichstein Nielsen wrote:
> > Thomas 'PointedEars' Lahn <PointedE...@web.de> writes:
> >> 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.


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

Tim Down 11-02-2009 12:44 AM

Re: Problem with getSelection() in iframe in chrome
 
On 31 Oct, 11:29, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> Lasse Reichstein Nielsen wrote:
> > Thomas 'PointedEars' Lahn <PointedE...@web.de> writes:
> >> 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.


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

Thomas 'PointedEars' Lahn 11-02-2009 05:00 PM

Re: Problem with getSelection() in iframe in chrome
 
Tim Down wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Lasse Reichstein Nielsen wrote:
>> > Thomas 'PointedEars' Lahn <PointedE...@web.de> writes:
>> >> 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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16


All times are GMT. The time now is 05:44 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.