开发者

JavaScript - How to get the current selection 'index' within a document

My code here returns a JavaScript selection object from within an iFrame (the iFrame page is within the same domain, so no xss issue).

What I need to know is the index of the selection within the raw html code (not the dom).

UPDATE:

E.g.

If you have an html doc:

<html><body>ABC</body></html>

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.

UPDATE 2

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.

// 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;
        }


You can access the index and offset of your selection by using selection.anchorOffset and selection.focusOffset.

Take a look at this: http://help.dottoro.com/ljjmnrqr.php

And here's another well explaned article: http://www.quirksmode.org/dom/range_intro.html

update to your update: I'm not sure why you're trying to get the index of the raw HTML code. But you can walk the DOM based on the selection kinda like this:

selection.anchorNode.nodeValue.replace(selection.anchorNode.nodeValue.substring(selection.anchorOffset, selection.focusOffset), 'replace value')

Note that it's still possible that anchorOffset is before focusOffset, based on whether you selected the text from left to right or from right to left.


If I understand correctly, you're looking to move around in the DOM. In that case, you can use these methods/properties:

  • parentNode
  • getChildNodes()
  • firstChild and lastChild

...and these links might help:

http://www.codingforums.com/archive/index.php/t-81035.html

http://www.sitepoint.com/forums/showthread.php?t=586034

The fastest way is probably

var node = document.getElementById('myElement'); alert(node.parentNode.indexOf(node));

(Sorry, for some reason the formatting buttons aren't showing up in my "Your Answer" area...)


I would be surprised if that information was available.

No DOM API is going to let you distinguish between

<html><body>ABC</body></html>

and

<html    ><body      >ABC</body></html>

The index in the raw HTML is different in each case, but the constructed DOM is identical.


You can't do this sensibly: the only possible method is to re-download the page's HTML via Ajax, parse the HTML and match the resulting DOM against the current DOM, which may itself have been altered by JavaScript. Besides, it's not a useful number anyway because once the page has been loaded, the original HTML string simply no longer exists in the DOM so offsets within that string have no meaning in JavaScript. Getting the selection in terms of nodes and offsets is much more sensible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜