Problem with getSelection() in iframe in chrome

Discussion in 'Javascript' started by Ciaran, Oct 30, 2009.

  1. Ciaran

    Ciaran Guest

    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
    Ciaran, Oct 30, 2009
    #1
    1. Advertising

  2. Ciaran

    Tim Down Guest

    On Oct 30, 3:55 am, Ciaran <> 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/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
    Tim Down, Oct 30, 2009
    #2
    1. Advertising

  3. Ciaran

    Ciaran Guest

    On 30 Oct, 11:54, Tim Down <> wrote:
    > On Oct 30, 3:55 am, Ciaran <> 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/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
    Ciaran, Oct 30, 2009
    #3
  4. 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$>
    Thomas 'PointedEars' Lahn, Oct 30, 2009
    #4
  5. Thomas 'PointedEars' Lahn <> 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'
    Lasse Reichstein Nielsen, Oct 30, 2009
    #5
  6. Lasse Reichstein Nielsen wrote:

    > Thomas 'PointedEars' Lahn <> 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
    Thomas 'PointedEars' Lahn, Oct 31, 2009
    #6
  7. Ciaran

    Ciaran Guest

    On 31 Oct, 11:29, Thomas 'PointedEars' Lahn <>
    wrote:
    > Lasse Reichstein Nielsen wrote:
    > > Thomas 'PointedEars' Lahn <> 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
    Ciaran, Nov 1, 2009
    #7
  8. Ciaran

    Tim Down Guest

    On 31 Oct, 11:29, Thomas 'PointedEars' Lahn <>
    wrote:
    > Lasse Reichstein Nielsen wrote:
    > > Thomas 'PointedEars' Lahn <> 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, Nov 2, 2009
    #8
  9. Ciaran

    Tim Down Guest

    On 31 Oct, 11:29, Thomas 'PointedEars' Lahn <>
    wrote:
    > Lasse Reichstein Nielsen wrote:
    > > Thomas 'PointedEars' Lahn <> 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, Nov 2, 2009
    #9
  10. Tim Down wrote:

    > Thomas 'PointedEars' Lahn wrote:
    >> Lasse Reichstein Nielsen wrote:
    >> > Thomas 'PointedEars' Lahn <> 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
    Thomas 'PointedEars' Lahn, Nov 2, 2009
    #10
  11. Ciaran

    Ciaran Guest

    Ciaran, Nov 25, 2009
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Crowe
    Replies:
    1
    Views:
    4,444
    Andrew Crowe
    Sep 13, 2004
  2. John Allsopp

    Safari window.getSelection()

    John Allsopp, May 13, 2004, in forum: Javascript
    Replies:
    0
    Views:
    108
    John Allsopp
    May 13, 2004
  3. Jason Quinn

    possible to getSelection from AJAX sites?

    Jason Quinn, Apr 18, 2007, in forum: Javascript
    Replies:
    1
    Views:
    82
    Peter Michaux
    Apr 21, 2007
  4. riog
    Replies:
    1
    Views:
    202
  5. riog

    window.getSelection on a textbox

    riog, Oct 17, 2007, in forum: Javascript
    Replies:
    1
    Views:
    102
    Thomas 'PointedEars' Lahn
    Oct 17, 2007
Loading...

Share This Page