Javascript-controlled clipboard

Source: Internet
Author: User
See UBB Code [Code] The "Copy to clipboard" function is available in windows. I think it is very useful.
After a file is uploaded, add a copy button to the URL that appears at the "address (PATH)" location.
I studied the original code ..
Define functions

ProgramCode view plaincopy to clipboardprint?

  1. FunctionCopytext (OBJ ){
  2. Ie = (document. All )?True:False
  3.  If(IE ){
  4. VaRRNG = Document. Body. createTextRange ();// List all text objects
  5. RNG. movetoelementtext (OBJ );// Move the text range so that the start and end positions of the range can fully contain the text of the given element
  6. RNG. scrollintoview ();// Scrollintoview: Scroll the object to the visible range and arrange it
  7. Top or bottom of the window
  8. RNG. Select ();// Select
  9. Rng.exe ccommand ("Copy");// Copy
  10. RNG. Collapse (False);// Not quite clear ..
  11. }
  12. }

function copytext (OBJ) {<br/> Ie = (document. all )? True: false <br/> If (IE) {<br/> var RNG = document. body. createTextRange (); // list all text object content <br/> RNG. movetoelementtext (OBJ); // move the text range so that the start and end positions of the range can fully contain the text of the given element <br/> RNG. scrollintoview (); // scrollintoview the object to the visible range and arrange it to the top or bottom of the <br/> window <br/> RNG. select (); // select <br/> rng.exe ccommand ("copy"); // copy <br/> RNG. collapse (false); // not clear .. <br/>}< br/>
then use
program code
[Copy code to clipboard]

code...

It mainly selects the Text object to be copied, and then uses execommand ("copy") to copy.
After that, I took a photo of the class and found that the content in <input type = text> could not be copied... only the outer box could be selected ..
I found some information on the internet... I finally got the answer ..
========================================================== =
Javascript tip (1) operate the clipboard
Javascript allows you to easily operate the client clipboard content, but it is only applicable to ie5 and later browsers.
Javascript can use the window. clipboardData object to process the clipboard content.
Method for saving to clipboard setdata (param1, param2)
Param1: data type, such as text or URL.
Param2: data content

Getdata (param1)
Data clearing method cleardata (param1)

The following is an example

Program code view plaincopy to clipboardprint?

  1. <Html>
  2. <Head>
  3. <Title>Test Operation clipboard</Title>
  4. </Head>
  5. <Script>
  6. Function copytoclipboard ()
  7. {
  8. VaRD=Document. All ("Source"). value;
  9. Window. clipboardData. setdata ('text', d );
  10. }
  11. </Script>
  12. <Body >
  13. <Button Onclick="Copytoclipboard ();">Copy</Button>
  14. <Input Type="Text" Size=20 ID="Source" Value="Test Data">
  15. <BR>
  16. <Button Onclick="Alert (window. clipboardData. getdata ('text '));">Display</Button>
  17. <Button Onclick="Window. clipboardData. cleardata ('text ');">Clear</Button>
  18. </Body>
  19. </Html>

<HTML> <br/> <pead> <br/> <title> test operation clipboard </ title> <br/> </pead> <br/> <SCRIPT> <br/> function copytoclipboard () <br/>{< br/> var d = document. all ("Source "). value; <br/> window. clipboardData. setdata ('text', d ); <br/>}< br/> </SCRIPT> <br/> <body> <br/> <button onclick = "copytoclipboard (); "> copy </button> <br/> <input type =" text "size = 20 id =" Source "value =" Test Data "> <br/> <br> <br/> <button onclick = "alert (window. clipboardData. getdata ('text'); "> display </button> <br/> <button onclick =" window. clipboardData. cleardata ('text '); "> clear </button> <br/> </body> <br/> </ptml> <br/>
The following is an example implementation page. selected characters, and drag it to the hosts area
note the window. event. the datatransfer object can also process the clipboard content, but it can only be used in the drag-and-drop operation

Program code view plaincopy to clipboardprint?

  1. <Html>
  2. <Head>
  3. <Title>Test Operation clipboard 2</Title>
  4. </Head>
  5. <Script>
  6. Function transferdrop (){
  7. WindowWindow. event. srcelement. innertext= Window. event. datatransfer. getdata ("text ");
  8. Window. event. returnvalue=False;
  9. }
  10. Function transferdrag (){
  11. Window. event. datatransfer. dropeffect='Move';
  12. Window. event. returnvalue=False;
  13. }
  14. </Script>
  15. <Body
  16. <P ID="Mysource" Ondragstart="Window. event. datatransfer. effectallowed=
  17. 'Move';">Select us and drag us to the textarea below</P>
  18. <Textarea ID="Mytarget" Ondrop="Transferdrop ();"
  19. Ondragover="Window. event. returnvalue = false ;" Ondragenter="Transferdrag ();">
  20. </Textarea>
  21. </Body>
  22. </Html>

<Textarea class = "html" style = "display: none "name =" code "rows =" 15 "Cols =" 100 "> <HTML> <br/> <pead> <br/> <title> test operation clipboard 2 </title> <br/> </pead> <br/> <SCRIPT> <br/> function transferdrop () {<br/> window. event. srcelement. innertext = Window. event. datatransfer. getdata ("text"); <br/> window. event. returnvalue = false; <br/>}< br/> function transferdrag () {<br/> window. event. datatransfer. dropeffect = 'move '; <br/> window. event. returnvalue = false; <br/>}< br/> </SCRIPT> <br/> <body <br/> <p id = "mysource" ondragstart = "window. event. datatransfer. export tallowed = <br/> 'move '; "> select and drag us to the following textarea </P> <br/> <textarea id =" mytarget "ondrop =" transferdrop (); "<br/> ondragover =" window. event. returnvalue = false; "ondragenter =" transferdrag (); "> <br/> </textarea> <br/> </body> <br/> </ptml> <br/> </textarea>
====================================
Discovery and use
Window. clipboardData. setdata ("text", value)
Window. clipboardData. getdata ("text ")
Window. clipboardData. cleardata ("text ")
It's easy to control the content of the clipboard ..

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.