1. Click the button to copy the content in the text box
<script type="text/javascript">function copyUrl2(){var Url2=document.getElementById("biao1");Url2.select(); // Select object document.execCommand("Copy"); // Execute the browser copy command alert("Copy, you can paste.");}</script><textarea cols="20" rows="10" id="biao1">User-defined code area</textarea><input type="button" onClick="copyUrl2()" value="click to copy code" />2. Copy the topic address and url address and pass it to friends on QQ/MSN
<!DOCTYPE html PUBLIC "-//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 the topic address and url address and pass it to friends on QQ/MSN"><script language="javascript">function copyToClipBoard(){var clipBoardContent="";clipBoardContent+=document.title;clipBoardContent+="";clipBoardContent+=this.location.href;window.clipboardData.setData("Text",clipBoardContent);alert("Copy successfully, please paste it on your QQ/MSN recommended to your friends");}</script>3. Directly copy the url
<input type="button" name="anniu2" onClick='copyUrl()' value="Copy URL address"><script language="javascript">function copyUrl(){var clipBoardContent=this.location.href;window.clipboardData.setData("Text",clipBoardContent);alert("Copy successful!");}</script>4. When clicking on the text box, copy the contents in the text box.
<input onclick="oCopy(this)" value="Hello.To copy content!"><script language="javascript">function oCopy(obj){obj.select();js=obj.createTextRange();js.execCommand("Copy")alert("Copy successful!");}</script>5. Copy the content in the text box or hide the field
<script language="javascript">function CopyUrl(target){target.value=myimg.value;target.select(); js=myimg.createTextRange(); js.execCommand("Copy");alert("Copy successful!");}function AddImg(target){target.value="[IMG]"+myimg.value+"[/ img]";target.select();js=target.createTextRange(); js.execCommand("Copy");alert("Copy successful!");}</script>6. Copy the contents in the span tag
<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 successful!");}</script>7. Browser compatible with copyToClipboard("Copy content")
function copyToClipboard(txt) { if (window.clipboardData) { window.clipboardData.clearData(); clipboardData.setData("Text", txt); alert("Copy successfully!"); } else if (navigator.userAgent.indexOf("Opera") != -1) { window.location = txt; } else if (window.netscape) { try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); } catch (e) { alert("Rejected by the browser! /nPlease enter 'about:config' in the browser address bar and press Enter /n and set 'signed.applets.codebase_principal_support' to 'true'"); } var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard); if (!clip) return; var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable); 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, copyright.length * 2); var clipid = Components.interfaces.nsIClipboard; if (!clip) return false; clip.setData(trans, null, clipid.kGlobalClipboard); alert("Copy successfully! "); } }8. Compatible with copy codes for major browsers (combined with ZeroClipboard.js)
<html><head><title>Zero Clipboard Test</title><script type="text/javascript" src="ZeroClipboard.js"></script><script language="JavaScript"> var clip = null; function $(id) { return document.getElementById(id); } function init() { clip = new ZeroClipboard.Client(); clip.setHandCursor(true); clip.addEventListener('mouseOver', function (client) { // update the text on mouse over clip.setText( $('fe_text').value ); }); clip.addEventListener('complete', function (client, text) { //debugstr("Copied text to clipboard: " + text ); alert("This address has been copied, you can paste it with Ctrl+V."); }); clip.glue('clip_button', 'clip_container' ); }</script></head><body onLoad="init()"><input id="fe_text" cols=50 rows=5 value=Copy content text1 ><span id="clip_container"><span id="clip_button"><b>Copy</b></span></span></body></html>