Posted By

multani on 05/25/08


Tagged

javascript clipboard copt


Versions (?)

Who likes this?

25 people have marked this snippet as a favorite

ashloren
luman
SpinZ
ischenkodv
alvaroisorna
korzhik
wizard04
jamesming
rafael
hans
jeremyhcobb
loric
palimadra
ckayra
liamchapman
dinkan
seanpowell
dekehooper
aegony
lfcortes
banjomamo
scilec
sbcjr
daisaru11
ringo380


javascript copy text to clipborad


 / Published in: JavaScript
 

  1. function copyToClipboard(txt) {
  2. if(window.clipboardData) {
  3. window.clipboardData.clearData();
  4. window.clipboardData.setData("Text", txt);
  5. } else if(navigator.userAgent.indexOf("Opera") != -1) {
  6. window.location = txt;
  7. } else if (window.netscape) {
  8. try {
  9. netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  10. } catch (e) {
  11. alert("被瀏覽器拒絕!\n請在瀏覽器地址欄輸入'about:config'並回車\n然後將'signed.applets.codebase_principal_support'設置為'true'");
  12. }
  13. var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
  14. if (!clip)
  15. return;
  16. var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
  17. if (!trans)
  18. return;
  19. trans.addDataFlavor('text/unicode');
  20. var str = new Object();
  21. var len = new Object();
  22. var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
  23. var copytext = txt;
  24. str.data = copytext;
  25. trans.setTransferData("text/unicode",str,copytext.length*2);
  26. var clipid = Components.interfaces.nsIClipboard;
  27. if (!clip)
  28. return false;
  29. clip.setData(trans,null,clipid.kGlobalClipboard);
  30. }
  31. alert('已經複製');
  32. }
  33. </script>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: sergiozoumenou on June 24, 2009

hi thanks for the code .i ma using the code below to copy to clipboard in mozilla and have a buton that call the fuction to copy. how can i paste in a field with a button that will call a fucntion like "fonction pastefromclipborad (aaaa).

My code :

function copy_clip(meintext){ if (window.clipboardData){ window.clipboardData.setData("Text", meintext); }else if (window.netscape){ netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard); if (!clip) return;

var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable); if (!trans) return;

trans.addDataFlavor('text/unicode');

var str = new Object(); var len = new Object();

var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString); var copytext=meintext; str.data=copytext; trans.setTransferData("text/unicode",str,copytext.length*2); var clipid=Components.interfaces.nsIClipboard;

if (!clip) return false; clip.setData(trans,null,clipid.kGlobalClipboard);

}

return false; } //-->

Posted By: sergiozoumenou on June 24, 2009

hi thanks for the code .i ma using the code below to copy to clipboard in mozilla and have a buton that call the fuction to copy. how can i paste in a field with a button that will call a fucntion like "fonction pastefromclipborad (aaaa).

My code :

function copy_clip(meintext){ if (window.clipboardData){ window.clipboardData.setData("Text", meintext); }else if (window.netscape){ netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard); if (!clip) return;

var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable); if (!trans) return;

trans.addDataFlavor('text/unicode');

var str = new Object(); var len = new Object();

var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString); var copytext=meintext; str.data=copytext; trans.setTransferData("text/unicode",str,copytext.length*2); var clipid=Components.interfaces.nsIClipboard;

if (!clip) return false; clip.setData(trans,null,clipid.kGlobalClipboard);

}

return false; } //-->

You need to login to post a comment.