`

Javascript Copy Text to Clipboard

阅读更多

Javascript clipboardData Object provides the functionality to use windows clipboard to copy text using javascript function. You can use the member methods of clipboardData object supported by Javascript to copy text from the HTML document elements. clipboardData object supports 3 methods:

  • setData

  • getData

  • clearData

 

setData method of javascript clipboardData object accepts two parameter values as argument. First takes the value as format of data and second parameter value as source of data or text that is to be copied. First parameter for format type can have two types of values one as Text and other as Url. setData method returns Boolean type value based on the successful execution to copy text on clipboard.

 

getData method of javascript clipboardData object accepts one parameter as format type of data stored on clipboard i.e. Text or Url.

 

clearData method of javascript clipboardData object clears the data stored on the clipboard. clearData method doesn’t take any parameter value to execute.

 

 

<script type="text/javascript" language="javascript"> 

function copyText() 
{ 

var copyDivText = document. getElementById( 'div1' ) .innerText; 
  

var returnVal = window. clipboardData. setData( 'Text', copyDivText ); 
  

alert(returnVal); 

  

document. getElementById( 'div2' ). innerText = window. clipboardData. getData( 'Text' ); 
} 

</script> 

<div id="div1"> 

Copy this text to the clipboard using javascript 

</div> 

<br /> 

<input type="button" id="btn" value="copy" onclick="copyText()"> 

<br /> 

<div id="div2"> 

</div> 

 

 

Javascript copy text to clipboard uses the clipboardData objects that consume the system clipboard utility to store the data until new data is being copied on the system clipboard. You can paste the stored data on clipboard multiple times.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics