Using the Clipboard


/ Published in: JavaScript
Save to your folder(s)

Functions to use the clipboard.

Requires a Flash file for non-IE browsers: see [http://javascript.internet.com/forms/clipboard-copy.html](http://javascript.internet.com/forms/clipboard-copy.html)
Remember to set `config.Clipboard.pathToFlash` accordingly.

`Clipboard.copy(strToCopy)` puts strToCopy onto the clipboard
`Clipboard.copySelection()` puts the text that the user has selected onto the clipboard
`Clipboard.paste()` returns the text from the clipboard (only works in IE)
`Clipboard.clear()` clears the clipboard (or sets it to a period if it can't set an empty string)


Copy this code and paste it in your HTML
  1. if(!window.config) var config = {};
  2. config.Clipboard = {
  3. pathToFlash: "/testsite/generic scripts/clipboard.swf",
  4. //see http://javascript.internet.com/forms/clipboard-copy.html
  5. //SWF file can be downloaded at http://javascriptsource.com/forms/clipboardFlash.zip
  6. flashCopierID: "flashCopier"
  7. };
  8.  
  9. //static Clipboard object
  10. var Clipboard = function()
  11. {
  12. //copies a string to the clipboard
  13. function copy(strToCopy)
  14. {
  15. if(!strToCopy) return false;
  16.  
  17. if(window.clipboardData && window.clipboardData.setData("text", strToCopy)) return true; //IE
  18.  
  19. var holder = document.createElement("textarea");
  20. holder.innerText = strToCopy;
  21. if(holder.createTextRange && holder.createTextRange().execCommand("copy")) return true; //IE
  22.  
  23. return flashCopy(strToCopy); //use the Flash copier
  24. }
  25.  
  26. //copies the currently selected text to the clipboard
  27. function copySelection()
  28. {
  29. var selectionText = "";
  30. //order matters here: Opera sparcely supports the TextRange object (IE) as well
  31. if(window.getSelection) selectionText = window.getSelection(); //(Selection object).toString()
  32. if(document.selection) selectionText = document.selection.createRange().text; //IE; (TextRange object).text
  33.  
  34. return copy(selectionText);
  35. }
  36.  
  37. //use a Flash file to copy to clipboard (assuming Flash is enabled in the browser, of course)
  38. //see http://javascript.internet.com/forms/clipboard-copy.html
  39. //SWF file can be downloaded at http://javascriptsource.com/forms/clipboardFlash.zip
  40. function flashCopy(strToCopy)
  41. {
  42. if(!strToCopy || !window.config.Clipboard.pathToFlash) return false;
  43.  
  44. var holder = document.getElementById(window.config.Clipboard.flashCopierID);
  45. if(!holder)
  46. {
  47. holder = document.createElement("div");
  48. holder.id = window.config.Clipboard.flashCopierID;
  49.  
  50. //holder.style.display = "none"; //the copy fails with this
  51. holder.style.position = "absolute";
  52. holder.style.zIndex = "-1";
  53.  
  54. document.body.appendChild(holder);
  55. }
  56. holder.innerHTML = '<embed src="'+window.config.Clipboard.pathToFlash+'" FlashVars="clipboard='+
  57. escape(strToCopy)+'" type="application/x-shockwave-flash"></embed>';
  58.  
  59. return null; //no way to know whether it was successful or not
  60. }
  61.  
  62. //returns the contents of the clipboard
  63. function paste()
  64. {
  65. if(window.clipboardData) //IE
  66. {
  67. return window.clipboardData.getData("text") || "";
  68. }
  69. else
  70. {
  71. var textarea = document.createElement("textarea");
  72. if(textarea.createTextRange) //IE
  73. {
  74. textarea.createTextRange().execCommand("paste");
  75. return textarea.innerText;
  76. }
  77. }
  78. return "";
  79. }
  80.  
  81. //clears the clipboard or sets it to a period if it can't be cleared
  82. function clear()
  83. {
  84. if(window.clipboardData) //IE
  85. {
  86. window.clipboardData.clearData();
  87. return true;
  88. }
  89. else return copy(".");
  90. }
  91.  
  92. return {
  93. copy: copy,
  94. copySelection: copySelection,
  95. paste: paste,
  96. clear: clear
  97. };
  98. }(); //initialize Clipboard

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.