Note: IE7 and IE8 have copy permissions for web pages, which need to be set in the "Custom Level" script in "Security"
clipboardData object
Provides access to the clipboard.
three methods
1.clearData(sDataFormat) deletes the data in the specified format from the clipboard.
2.getData(sDataFormat) Gets data in the specified format from the clipboard.
3.setData(sDataFormat, sData) assigns data in the specified format to the clipboard. Returning true indicates the operation was successful.
example
<script language="JavaScript">
<!--
var text = "123";
if (!window.clipboardData.setData('Text', text)) // Assign data in text format
{
alert("Copy failed!");
}
text = window.clipboardData.getData('Text'); // Get data in text format
alert(text);
window.clipboardData.clearData('Text'); // Clear text format data
text = window.clipboardData.getData('Text');
alert(text);
//-->
</script>
Some methods:
<!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 send them 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("Copied successfully, please paste it into your QQ/MSN and recommend it to your friends");
}
</script>
<br />
<br />
Copy the url directly
<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 successfully!");
}
</script>
<br/>
<br/>
When clicking the text box, copy the content inside the text box
<input onclick="oCopy(this)" value="Hello. The content you want to copy!">
<script language="javascript">
function oCopy(obj){
obj.select();
js=obj.createTextRange();
js.execCommand("Copy")
alert("Copy successfully!");
}
</script>
<br />
<br />
Copy the contents of a text box or hidden field
<script language="javascript">
function CopyUrl(target){
target.value=myimg.value;
target.select();
js=myimg.createTextRange();
js.execCommand("Copy");
alert("Copy successfully!");
}
function AddImg(target){
target.value="[IMG]"+myimg.value+"[/ img]";
target.select();
js=target.createTextRange();
js.execCommand("Copy");
alert("Copy successfully!");
}
</script>
<input name=myimg type=hidden id=myimg value="http://pmp.www.VeVB.COm" />
<input name=imgurl type=text size=32 value="http://pmp.www.VeVB.COm" />
<input type=button value="Click here to copy this website address" onclick="CopyUrl(imgurl);" />
<br />
<br/>
Copy content in 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 successfully!");
}
</script>
Here is the code snippet:< br />
<br />
<span id="tbid">http://pmp.www.VeVB.COm</span>
[<a href="#" onclick="copyText(document.all.tbid)">Click to copy</a>]<br/><br/>
<span id="tbid2">http://www.www.VeVB.COm/pmp</span>
[<a href="#" onclick="copyText(document.all.tbid2)">Click to copy</a>]<br/><br/>
</p>
</body>
</html>
There is another way:
function copyQQ(qq){
var obj=document.getElementById(qq);
obj.select();
js=obj.createTextRange();
js.execCommand("Copy");
alert("The code has been copied successfully!");
}
//Set the copied content and append the URL of this website
function SetCopyContent() {
window.event.returnValue = false;
var content = document.title + "/r/n";
content += document.getElementById("txt1").value + "/r/n";
content += "This resource comes from" + this.location.href;
window.clipboardData.setData('Text', content);
alert("Copied successfully, please paste it into your QQ/MSN and recommend it to your friends");
}
Call:
<input id="txt1" type="text" value="Hello World!" onclick="getTxtSelect(event)"/>
<input type="button" value="Copy the value in the text box" onclick="SetCopyContent();" />
Copy the code code as follows:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Properties of Window object 02</title>
<script language="javascript" type="text/javascript">
/*
window.screen object: Screen object, containing screen-related information.
window.clipboardData object: clipboard object, object for clipboard operations. (Only copying or formatting text can be supported within the web page)
clearData("Text"): Clear the clipboard
getData("Text"): Read the value of the clipboard. Only Text text format is supported in IE.
setData("Text",value): Set the value in the clipboard
Case: Copy the address to a friend, see notes
Disable copying: set return false in the body oncopy event;
oncopy, onpase events: copy and paste events, can be used for most controls
*/
//Screen resolution
function screenInfo() {
if (window.screen.width < 1024 || window.screen.height < 768) {
window.alert("Your computer is a prehistoric product!");
return;
}
window.alert("Your resolution is: " + window.screen.width + " " + window.screen.height);
}
//Copy address to friend
function operClipBoard() {
var divObj = document.getElementById("divClipBoard");
var content = divObj.innerText;
content = "The content you copied is:" + content + "/r/n Resource source:" + window.location.href;
window.alert('Copy successfully!');
//This attribute will only display the content after the customer has copied it.
window.alert(window.clipboardData.getData("text"));
window.clipboardData.setData("Text",content);
}
//Web pages are prohibited from copying
function forbidCopy() {
window.alert("The content of the web page can be viewed but cannot be moved!");
return false;
}
</script>
</head>
<body onload="screenInfo();" oncopy="forbidCopy();" >
<form id="form1" runat="server">
<div id="divClipBoard" onclick="operClipBoard();" >
//www.VeVB.COm
</div>
<hr />
Enter password:
<input type="text" oncopy="window.alert('Copying prohibited!');return false;" />
Enter your password again:
<input type="text" onpaste="window.alert('Paste prohibited!');return false;" />
</form>
</body>
</html>
Copy the code code as follows:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">
/*
div has no oncopy event
body and text box have this event
*/
function OperClipBoard() {
window.clipboardData.setData("Text", window.clipboardData.getData("Text") + "/r/nThis resource comes from:" + window.location.href);
}
/*
Process: Oncopy is triggered first. After triggering, it only copies the content to the pasteboard. If it needs to be processed twice, wait for the content to be copied to the pasteboard and then perform two operations, that is, after processing the value,
During assignment operation
*/
function copyContent()
{
window.setTimeout("OperClipBoard()", 100);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div oncopy="copyContent();">
Hello MyJSWorld!
</div>
<br />
<input type="text" oncopy="OperClipBoard();" value="Hello MyJSWorld!" />
</form>
</body>
</html>