Programming/Javascript

How to Copy an Image to the Clipboard??

Figo Kim 2009. 6. 4. 13:21
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

<html>
<head>
<title></title>
<script type="text/javascript">
function fnCopy(objId) {
   var imgObj = document.getElementById('imgId');
   imgObj.contentEditable = 'true';
   var controlRange;
   if (document.body.createControlRange) {
      controlRange = document.body.createControlRange();
      controlRange.addElement(imgObj);
      controlRange.execCommand('Copy');
      alert("Copy image done");
   }
   imgObj.contentEditable = 'false';
}
</script>
</head>

<body>

<div id="test1">
   <img id="imgId" src="http://www.google.com/intl/en_ALL/images/logo.gif" />
</div>

<a href="javascript:fnCopy('test1');">Copy Image</a>
</body>
</html>


  • contentEditable : Controls whether the element is editable by the user via the IE for Windows live content-editing facilities.
  • execCommand : Executes a command on the current document, current selection, or the given range.

  • createControlRange : Creates a selection range object for control-based selection rather than text-based selection. This returns a pointer to the element created.


I don't think it's a stardard, but it's pretty useful~~~~~