Js clipboard application clipboardData detailed resolution

  • 2020-03-30 00:54:06
  • OfStack

Note: ie7, and ie8 have a copy of the page permissions, to be set in the "security" in the "custom level" script

The clipboardData object

Provides access to the clipboard.

Three methods
1. ClearData (sDataFormat) deletes data in a specified format from the clipboard.
GetData (sDataFormat) gets the data in the specified format from the clipboard.
3. SetData (sDataFormat, sData) assigns a specified format of data to the clipboard. Returning true indicates that the operation was successful.

example

< Script language = "JavaScript" >
< ! --
Var text = "123";
If (! Window. The clipboardData. SetData (' Text, Text)) / / give the Text format of the data
{
        Alert (" copy failed!" );
}
Text = window. ClipboardData. GetData (" text "); // get the data in text format
Alert (text);
Window. The clipboardData. ClearData (" Text "); // clears data in text format
Text = window. ClipboardData. GetData (" text ");
Alert (text);
/ / - >
< / script>

Some ways:

< ! PUBLIC DOCTYPE HTML "- / / / / W3C DTD XHTML 1.0 Transitional / / EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< HTML XMLNS = "http://www.w3.org/1999/xhtml" >
< Head>
< Meta HTTP - equiv = "content-type" Content = "text/HTML. Charset = gb2312 "/ >
< Title> Js copy code < / title>
< / head>
< Body>
< P>
< Input type="button" name="anniu1" onClick='copyToClipBoard()' value=" copy theme address and url address to QQ/MSN friends ">
< Script language = "javascript" >
    The function copyToClipBoard () {
      Var clipBoardContent = "";
      ClipBoardContent + = document. The title;
      ClipBoardContent + = "";
      ClipBoardContent + = this. Location. Href;
      Window. The clipboardData. SetData (" Text ", clipBoardContent);
      Alert (" copy successfully, please paste it on your QQ/MSN to recommend it to your friends ");
}
< / script>    
< Br / >
< Br / >


Directly copy the url
< Input type="button" name="anniu2" onClick='copyUrl()' value=" copyUrl address ">
< Script language = "javascript" >
    The function copyUrl ()
    {
      Var clipBoardContent = this. Location. Href;
      Window. The clipboardData. SetData (" Text ", clipBoardContent);
      Alert (" copy successful!" );
    }
< / script>
< Br / >
< Br / >
When you click on the text box, copy the text box
< Input onclick="oCopy(this)" value=" hello. >
< Script language = "javascript" >
The function oCopy (obj) {
Obj. The select ();
Js = obj. CreateTextRange ();
Js. ExecCommand (" Copy ")
Alert (" copy successful!" );
}
< / script>
< Br / >
< Br / >
Copy the text box or hide the contents of the field
< Script language = "javascript" >
The function CopyUrl (target) {
Target, value = myimg. Value;
Target. The select ();    
Js = myimg. CreateTextRange ();    
Js. ExecCommand (" Copy ");
Alert (" copy successful!" );
}
The function AddImg (target) {
Target, value = "[IMG]" + myimg. Value + "[/ IMG]";
Target. The select ();
Js = target. CreateTextRange ();    
Js. ExecCommand (" Copy ");
Alert (" copy successful!" );
}
< / script>

< Input name=myimg type=hidden id=myimg value="http://pmp.www.jb51.net" />
< Input name=imgurl type=text size=32 value="http://pmp.www.jb51.net" />
< Input type=button value=" click here to copy the address of this site "onclick="CopyUrl(imgurl);" / >


< Br / >
< Br / >
Copy the contents of the span tag
< The script type = "text/javascript" >
< / script>
< Br / >
< Br / >
< The script type = "text/javascript" > The function copyText (obj)    
{
Var RNG = document. Body. CreateTextRange ();
RNG. MoveToElementText (obj);
RNG. ScrollIntoView ();
RNG. The select ();
RNG. ExecCommand (" Copy ");
RNG. The collapse (false);
Alert (" copy successful!" );
}
< / script>

Here is the code snippet: < Br / >
< Br / >
< Span id = "tbid" > http://pmp.www.jb51.net< / span>    
[< a href = "#" onclick = "copyText (document. All. Tbid)" > click copy < / a>] < Br / > < Br / >
< Span id = "tbid2" > http://www.www.jb51.net/pmp< / span>    
[< a href = "#" onclick = "copyText (document. All. Tbid2)" > click copy < / a>] < Br / > < Br / >

< / p>
< / body>
< / html>

Here's another way:

The function copyQQ (qq) {
Var obj = document. GetElementById (qq);
Obj. The select ();
        Js = obj. CreateTextRange ();
        Js. ExecCommand (" Copy ");
        Alert (" code has been successfully copied!" );

}


// set the copied content to be appended to the URL of this website
              The function SetCopyContent () {
                      Window. The event. The returnValue = false;
                      Var content = document.title + "/r/n";
                      Content += document.getelementbyid ("txt1"). Value + "/r/n";
                      Content += "this resource comes from" + this.location.href;
                      Window. The clipboardData. SetData (' Text 'content);
                      Alert (" copy successfully, please paste it on your QQ/MSN to recommend it to your friends ");
              }

Call:


Related articles: