Javascript implementation Copy (copy) action method Daquan _javascript techniques

Source: Internet
Author: User

One, the realization clicks the button, duplicates the content in the text box

<script type= "Text/javascript" >
function copyUrl2 ()
{
var Url2=document.getelementbyid ("Biao1") ;
Url2.select (); Select the object
document.execcommand ("copy");///Execute Browser Copy command
alert ("copied well, sticky.") ");
}
</script>
<textarea cols= "rows=" id= "Biao1" > user-defined code area </textarea>
<input type= "Button" onclick= "CopyUrl2 ()" value= "click Copy Code"/>

Second, copy the topic address and URL address, passed to qq/msn friend

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
 
 

When you click the text box, copy the contents of the text box

<input onclick= "ocopy (This)" value= "hello. To copy the content!" >
<script language= "javascript" >
function ocopy (obj) {
obj.select ();
Js=obj.createtextrange ();
Js.execcommand ("copy")
alert ("Replication succeeded!");
</script>

V. Copy text boxes or hide the contents of a field

<script language= "javascript" >
function Copyurl (target) {
target.value=myimg.value;
Target.select (); 
Js=myimg.createtextrange (); 
Js.execcommand ("Copy");
Alert ("Copy succeeded!");
}
function addimg (target) {
target.value= "[img]" +myimg.value+ "[/img]";
Target.select ();
Js=target.createtextrange (); 
Js.execcommand ("Copy");
Alert ("Copy succeeded!");
}
</script>

Vi. copying the contents of span tags

<script type= "Text/javascript" >
</script>
<br/>
<br/>
<script Type= "Text/javascript" >function copytext (obj) 
{
var rng = Document.body.createTextRange ();
Rng.movetoelementtext (obj);
Rng.scrollintoview ();
Rng.select ();
Rng.execcommand ("Copy");
Rng.collapse (false);
Alert ("Copy succeeded!");
}
</script>

Seven, browser compatible CopyToClipboard ("Copy content")

function CopyToClipboard (TXT) {if (window.clipboarddata) {window.clipboardData.clearData ();
        Clipboarddata.setdata ("Text", txt); Alert ("Copy succeeded!")
 
      ");
      else if (navigator.userAgent.indexOf ("Opera")!=-1) {window.location = txt; else if (Window.netscape) {try {netscape.security.PrivilegeManager.enablePrivilege ("Universalxpconne
        CT "); catch (E) {alert ("Rejected by browser!")
        \ nplease type ' About:config ' in the browser address bar and enter \ n then set ' Signed.applets.codebase_principal_support ' to ' true '); var clip = components.classes[' @mozilla. Org/widget/clipboard;1 '].createinstance (Components.interfaces.nsIClipboa
        RD);
        if (!clip) return; var trans = components.classes[' @mozilla. Org/widget/transferable;1 '].createinstance (
        Components.interfaces.nsITransferable);
        if (!trans) return;
        Trans.adddataflavor ("Text/unicode");
        var str = new Object ();
     var len = new Object ();   var str = components.classes["@mozilla. Org/supports-string;1"].createinstance (
        Components.interfaces.nsISupportsString);
        var copytext = txt;
        Str.data = CopyText;
        Trans.settransferdata ("Text/unicode", str, copytext.length * 2);
        var clipid = Components.interfaces.nsIClipboard;
        if (!clip) return false;
        Clip.setdata (trans, NULL, Clipid.kglobalclipboard); Alert ("Copy succeeded!")
      "); }
    }

Eight, compatible with the major browser replication code (combined with Zeroclipboard.js)

 

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.