开发者

Save highlighted text position in HTML using javascript

I'm able to select the text using window.getSelection() How can I get the character position of the start and end of the selection in the HTML co开发者_运维技巧de? So this information can be saved to the server.


I feel there is no built-in functions for getting start and end position of selection code. you need to write some JS coding for getting these position. I've wrote just simple coding. I'm not sure whether it will be useful or not. But, check it out.

<html>
    <head>
        <script>
            function GetSelectedText () 
            {
                var fullString = "I feel there is no built-in functions for getting start and end position of selection code. you need to write some JS coding for getting these position. I've wrote just simple coding. I'm not sure whether it will be useful or not. But, check it out.";
                if (window.getSelection) 
                    {               
                        var range = window.getSelection ();                     
                        var startPosition = fullString.search(range);
                        var getPosition = range.toString();
                        var endPosition = parseInt(getPosition.length) + parseInt(startPosition)
                        alert ("Start position : " + startPosition + " and End position : " + endPosition);        
                    }
                else
                    {
                        if (document.selection.createRange)
                        {
                            var range = document.selection.createRange ();
                            var startPosition = fullString.search(range.text);
                            var getPosition = range.text;
                            var endPosition = parseInt(getPosition.length) + parseInt(startPosition);
                            alert ("Start position : " + startPosition + " and End position : " + endPosition);
                        }
                    }       
            }
        </script> 
    </head>
    <body>
        <button onclick="GetSelectedText ()">
            Get 
        </button>
        I feel there is no built-in functions for getting start and end position of selection code. you need to write some JS coding for getting these position. I've wrote just simple coding. I'm not sure whether it will be useful or not. But, check it out.
    </body>
</html>

For this coding, you need to define your text message in two places. One is to in JS coding

var fullString = "??"

and another is to Body message below of Button/


I think the shorter way would be to use string.indexOf() function of javascript. It simply returns the starting index of the searched string in the larger string and -1 if it is not present. The end index can be calculated by adding the length of the substring with starting index.


You can't reliably get the offset of the user selection in terms of the original HTML source of the document. You can only get it in terms of nodes and offsets within nodes. I'd recommend looking at the following answer to a similar question: Calculating text selection offsets in nest elements in Javascript

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜