Use of javascript copy and paste and clipboardData, clipboarddata

Source: Internet
Author: User

Use of javascript copy and paste and clipboardData, clipboarddata

Window. clipboardData can be copied and pasted. Its getData method can be used to read data, and the setData method can be used to set data.

<Script language = "javascript"> function readTxt () {alert (window. clipboardData. getData ("text");} function setTxt () {var t = document. getElementById ("txt"); t. select (); window. clipboardData. setData ('text', t. createTextRange (). text) ;}</script> <input name = "txt" value = "test"> <input type = "button" value = "copy" onclick = "setTxt () "> <input type =" button "value =" read "onclick =" readTxt () ">

How Can I copy and paste data using javascript ???

You can use the: Object .createtextrange(.exe cCommand ("copy") command for copying. You can use:
Window. clipboardData. setData ("text", content source)
The source of the content here is to copy the text in the text box ID to the clipboard. Here, write y. value.

Paste is actually the value Attribute of the target object = y. value

Cutting is based on selection. You must first create a selection area. If the ID of a text box is a, you need:
A.createtextrange(cmd.exe cCommand ("cut ")
To complete the cut.

Select all. If the ID of a text box is B, you only need B. select () to select all text boxes.

How to copy and paste javascript

This function is good and has been made. It is a very practical small function. (In addition, this function is only for IE ):

Step 1: Set the favorites URL:

Javascript: function getSelectedText () {return document. selection. createRange (). text;} function insert () {var input = document. getElementsByTagName ("input"); input [0]. value = window. clipboardData. getData ('text')} if (getSelectedText ()! = "") {Window. clipboardData. setData ('text', getSelectedText (); location = "www.baidu.com"; void (0)} else {insert ()};

Step 2: Set your favorites name

Step 3: If you are using IE7 or later, change IE-> Tools-> Security-> Custom Level-> script-> Allow programmatic access to the clipboard to enable it;

--------

Usage:
1. Select your text on any page and click this favorite link,
2. The page will automatically go to the location page you set, and then you can click this favorite link;
3. as long as there is an input box in this page (if the first box is text, it is a small BUG if it is not easy to say anything else ), you can paste the content in the input box,

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.