JavaScript - How to get the current selection 'index' within a document
<p>My code here returns a JavaScript selection object from within an iFrame (the iFrame page is within the same domain, so no xss issue). </p>

<p>What I need to know is the index of the selection within the raw <strong>html code</strong> (not the dom).</p>

<p><strong>UPDATE:</strong></p>

<p>E.g.</p>

<p>If you have an html doc:</p>

<pre><code>&lt;html&gt;&lt;body&gt;ABC&lt;/body&gt;&lt;/html&gt;
</code></pre>

<p>And in the UI, the user uses their mouse to select the text 'ABC', I want to be able to use JavaScript to determine the postion of the selected text in the html source. In this case the index of ABC is 13.</p>

<p><strong>UPDATE 2</strong></p>

<p>The reason I'm persisting with this madness, is that I need to create a tool that can revisit a page and pull text based on a selected text the user has identified at an earlier time. The user tells the system where the text is, and the system from that point on uses regular expressions to pull the text. Now, if the dom is not the same as the raw html, and there's no way to pinpoint the selection in the raw html - it's really difficult to know what reg ex to generate. I don't think there's another way around this. </p>

<pre><code>// Returns the raw selection object currently
        // selected in the UI
        function getCurrentSelection() {

            var selection = null;

            var iFrame = document.getElementById('uc_iFrameGetPriceData');

            try {

                if (window.getSelection) { // Gecko

                    selection = iFrame.contentWindow.getSelection();
                }
                else { // IE

                    var iframeDoc = iFrame.contentWindow.document;

                    if (iframeDoc.selection) {

                        selection = iframeDoc.selection;
                    }
                    else {

                        selection = iframeDoc.contentWindow.getSelection();
                    }
                }
            }
            catch (err) {

                alert( 'Error: getCurrentSelection() - ' + err.description )

            }

            return selection;
        }
</code></pre>
