Tips for getting selected text on a page with JavaScript

Source: Internet
Author: User

One of the tips here is how to use JavaScript to get the selected text on the page. The most critical JavaScript APIs are:

Event= Window.  GetSelection();     

This selection is actually an object, but if we use it .toString() or force it into a string, we will get the selected text.

$ (document). Ready (function () {$ (". ContentText"). MouseUp (function (e) {var Txt;var parentoffset = $ (this). offset (); var x = E.pagex-parentoffset.left;var y = E.pagey-parentoffset.top;txt = Window.getselection (); if (txt.toString (). Length & Gt 1) {alert (TXT);});});

If we put this piece of code on the following page:

>><title>get selected text with JavaScript</title><metaCharSet="UTF-8"><metaName="Viewport"Content=The width=device-width, Initial-scale=1.0"><scriptSrc="Http://www.webhek.com/wordpress/wp-includes/js/jquery/jquery.js"Type="Text/javascript"></script>><body><divclass= "Contenttext" > and the client's JavaScript is different, PHP code is running on the server side. If you set up code similar to the example above on your server, the client will be able to receive the results after running the script, but they will not know how the code behind it works. You can even set the WEB server to allow PHP to process all the HTML files, so that the user doesn't know what the servers are doing. One of the great benefits of using PHP is that it is simple for beginners and provides a variety of advanced features for professional programmers. Don't be afraid when you see a long list of features in PHP. You can get started quickly and you can write some simple scripts yourself in a few hours. </div > </body>>        

When you use the mouse to select some of the text in the page, and you get the selected content, I am here to use the alert() method to display it.

Watch the demo watch demo from: Http://www.webhek.com/get-selected-text-with-javascript

Tips for getting selected text on a page with JavaScript

Related Article

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.