Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A simple workaround is not to serialize and send the <em>entire</em> select object, but rather to store the start and end points as XPaths (along with their offsets). Something like this would do:</p> <pre><code>function makeXPath (node, currentPath) { /* this should suffice in HTML documents for selectable nodes, XML with namespaces needs more code */ currentPath = currentPath || ''; switch (node.nodeType) { case 3: case 4: return makeXPath(node.parentNode, 'text()[' + (document.evaluate('preceding-sibling::text()', node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength + 1) + ']'); case 1: return makeXPath(node.parentNode, node.nodeName + '[' + (document.evaluate('preceding-sibling::' + node.nodeName, node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength + 1) + ']' + (currentPath ? '/' + currentPath : '')); case 9: return '/' + currentPath; default: return ''; } } function restoreSelection () { var selection = window.getSelection(); selection.removeAllRanges(); var range = document.createRange(); range.setStart(document.evaluate(selectionDetails[0], document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue, Number(selectionDetails[1])); range.setEnd(document.evaluate(selectionDetails[2], document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue, Number(selectionDetails[3])); selection.addRange(range); } } function getSelection() { var selection = window.getSelection(); var range = selection.getRangeAt(0); var selectObj = { 'startXPath': makeXPath(range.startContainer), 'startOffset': range.startOffset, 'endXPath': makeXPath(range.endContainer), 'endOffset': range.endOffset } return selectObj } </code></pre> <p>Not thoroughly tested, but the general idea is here.</p> <p>Sources: <a href="http://groups.google.com/group/mozilla.dev.tech.dom/tree/browse_frm/thread/fc8732ee11aba5cc/5116179f922387d0?rnum=11&amp;_done=/group/mozilla" rel="nofollow">This</a> and <a href="http://home.arcor.de/martin.honnen/javascript/storingSelection1.html" rel="nofollow">That</a></p>
    singulars
    1. This table or related slice is empty.
    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. 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