Details of clipboardData object usage in javascript

  • 2020-06-12 08:25:48
  • OfStack

This article illustrates the use of clipboardData objects in javascript. Share to everybody for everybody reference. The specific analysis is as follows:

clipboardData object, note that the web clipboard can only be set to Text, that is, to copy text
clearData("Text") empty the paste board
getData("Text") reads the value of the paste board
setData("Text",val) sets the value of the paste board

body's oncopy event is triggered when copying. return false is not allowed to copy
< body oncopy="alert(' Do not copy! '); return false;" >
Many elements also have oncopy,onpaste events

1. Copy text to clipboard


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title></title>
 <script type="text/javascript">
 function CopyLinkAddress() {
  clipboardData.setData("Text", " Please copy the url to your QQ:" + location.href);
  alert(" Copy success !");
 }
 </script>
</head>
<body>
 <input type="button" value=" Copy the url " onclick="CopyLinkAddress()" />
</body>
</html>

2. No copying and no pasting


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title></title>
 <script type="text/javascript">
 function CopyLinkAddress() {
  clipboardData.setData("Text", " Please copy the url to your QQ:" + location.href);
  alert(" Copy success !");
 }
 </script>
</head>
<!--<body oncopy="alert(' forbidding ');return false;">-->
<body>
 <input type="button" value=" Copy the url " onclick="CopyLinkAddress()" />
  Test the copied text <br />
  Mobile phone number 1:<input type="text" /><br />
  Mobile phone number 2:<input type="text" 
 onpaste="alert(' No pasting, must be done manually !');return false;" />
</body>
</html>

3. Add source when clipboardData object is copied


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title></title>
 <script type="text/javascript">
 function ModifyCopyData() {
  clipboardData.setData('Text',clipboardData.getData('Text') +
   '\r\n from Pigeon Web site ' + location.href);
 }
 </script>
</head>
<!-- Not directly in oncopy In the call ModifyCopyData function 
  You need to set a timer ,0.1 Seconds after implementation , So it's no longer oncopy The execution call stack went up 
-->
<body oncopy="setTimeout('ModifyCopyData()',100)">
  The home of the script :www.ofstack.com
</body>
</html>

Hopefully this article has been helpful in your C# programming.


Related articles: