Javascript Gets the text implementation code selected on the page

Source: Internet
Author: User
The code is as follows Copy Code

<script type= "Text/javascript" >

Description: Gets the selected text on the page


function Getselectedtext () {
if (window.getselection) {
This technique was the most likely to be standardized.
GetSelection () returns a Selection object, which we do not document.
Return Window.getselection (). toString ();
}
else if (document.getselection) {
This is the older, simpler technique that returns a string
return Document.getselection ();
}
else if (document.selection) {
This is the ie-specific technique.
We Don't document the IE selection property or TextRange objects.
Return Document.selection.createRange (). text;
}
}

</script>

Under FireFox to get input or textarea selected text, you can use the following method:

  code is as follows copy code

<script type=" Text/javascript ">
 
//Description: FireFox get input or The text selected in TextArea

 
Function gettextfieldselection (e) {
    if (E.selectionstart!= un Defined && e.selectionend!= undefined) {
        var start = e.selection Start;
        var end = E.selectionend;
        return e.value.substring (start, end);
   }
    else Return ""; //not supported on this browser
}
 
</script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.