Javascript implementation of the Copy action method
- 2020-03-30 03:24:13
- OfStack
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 the object
document.execCommand("Copy"); //Execute the browser copy command
alert(" It has been copied and can be pasted. ");
}
</script>
<textarea cols="20" rows="10" id="biao1"> User-defined code areas </textarea>
<input type="button" onClick="copyUrl2()" value=" I'm gonna hit copy code " />
2. Copy the thematic address and url address and send them to your 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 the code </title>
</head>
<body>
<p>
<input type="button" name="anniu1" onClick='copyToClipBoard()' value=" Copy topic address and url Address to QQ/MSN The good friend ">
<script language="javascript">
function copyToClipBoard(){
var clipBoardContent="";
clipBoardContent+=document.title;
clipBoardContent+="";
clipBoardContent+=this.location.href;
window.clipboardData.setData("Text",clipBoardContent);
alert(" Copy successful, please paste into yours QQ/MSN Recommend it to your friends ");
}
</script>
Third, 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 success !");
}
</script>
When you click on the text box, copy the text box
<input onclick="oCopy(this)" value=" hello . to copy The content of the !">
<script language="javascript">
function oCopy(obj){
obj.select();
js=obj.createTextRange();
js.execCommand("Copy")
alert(" Copy success !");
}
</script>
Copy the text box or hide the contents of the field
<script language="javascript">
function CopyUrl(target){
target.value=myimg.value;
target.select();
js=myimg.createTextRange();
js.execCommand("Copy");
alert(" Copy success !");
}
function AddImg(target){
target.value="[IMG]"+myimg.value+"[/ img]";
target.select();
js=target.createTextRange();
js.execCommand("Copy");
alert(" Copy success !");
}
</script>
Copy the content 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 success !");
}
</script>
7. Browser compatibility CopyToClipboard (" copy content ")
function copyToClipboard(txt) {
if (window.clipboardData) {
window.clipboardData.clearData();
clipboardData.setData("Text", txt);
alert(" Copy successful! ");
} 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 browser! n Please type in the browser address bar 'about:config' And press enter n then 'signed.applets.codebase_principal_support' Set 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 (!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 successful! ");
}
}
Viii. Copy code compatible with all 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(" The address has been copied, you can use it Ctrl+V Paste. ");
});
clip.glue('clip_button', 'clip_container' );
}
</script>
</head>
<body onLoad="init()">
<input id="fe_text" cols=50 rows=5 value= Copy content text 1 >
<span id="clip_container"><span id="clip_button"><b> copy </b></span></span>
</body>
</html>