Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update</strong></p> <p>I've written a cross-browser range and selection library called <a href="http://code.google.com/p/rangy" rel="nofollow noreferrer">Rangy</a> that incorporates an improved version of the code I posted below. You can use the <a href="http://code.google.com/p/rangy/wiki/SelectionSaveRestoreModule" rel="nofollow noreferrer">selection save and restore module</a> for this particular question, although I'd be tempted to use something like <a href="https://stackoverflow.com/questions/1181700/set-cursor-position-on-contenteditable-div/3323835#3323835">@Nico Burns's answer</a> if you're not doing anything else with selections in your project and don't need the bulk of a library.</p> <p><strong>Previous answer</strong></p> <p><strike>You can use IERange (<a href="http://code.google.com/p/ierange/" rel="nofollow noreferrer">http://code.google.com/p/ierange/</a>) to convert IE's TextRange into something like a DOM Range and use it in conjunction with something like eyelidlessness's starting point. Personally I would only use the algorithms from IERange that do the Range &lt;-> TextRange conversions rather than use the whole thing. And IE's selection object doesn't have the focusNode and anchorNode properties but you should be able to just use the Range/TextRange obtained from the selection instead.</p> <p>I might put something together to do this, will post back here if and when I do.</p> <p>EDIT:</p> <p>I've created a demo of a script that does this. It works in everything I've tried it in so far except for a bug in Opera 9, which I haven't had time to look into yet. Browsers it works in are IE 5.5, 6 and 7, Chrome 2, Firefox 2, 3 and 3.5, and Safari 4, all on Windows.</p> <p><a href="http://www.timdown.co.uk/code/selections/" rel="nofollow noreferrer">http://www.timdown.co.uk/code/selections/</a></p> <p>Note that selections may be made backwards in browsers so that the focus node is at the start of the selection and hitting the right or left cursor key will move the caret to a position relative to the start of the selection. I don't think it is possible to replicate this when restoring a selection, so the focus node is always at the end of the selection.</p> <p>I will write this up fully at some point soon.</strike></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload