copy text to clipboard with javascript

a very popular article about how to copy text to clipboard out of HTML with javascript in firefox/mozilla/netscape and IE

this is a javascript that i built out of several sources so that it is possible to copy text out of HTML in firefox/mozilla/netscape and in Internet Explorer

large parts of that script are copied from other sources, thats why there are dutch comments ;-)

this script only works in mozilla if you sign the javascript or if you change some settings in firefox… see notes below

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

// Copyright (C) krikkit - krikkit@gmx.net// --> http://www.krikkit.net///// This program is free software; you can redistribute it and/or// modify it under the terms of the GNU General Public License// as published by the Free Software Foundation; either version 2// of the License, or (at your option) any later version.

function copy_clip(meintext){ if (window.clipboardData)    {

   // the IE-manier   window.clipboardData.setData("Text", meintext);

   // waarschijnlijk niet de beste manier om Moz/NS te detecteren;   // het is mij echter onbekend vanaf welke versie dit precies werkt:   }   else if (window.netscape)    { 

   // dit is belangrijk maar staat nergens duidelijk vermeld:   // you have to sign the code to enable this, or see notes below    netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

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

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

   // specificeer wat voor soort data we op willen halen; text in dit geval   trans.addDataFlavor('text/unicode');

   // om de data uit de transferable te halen hebben we 2 nieuwe objecten    // nodig om het in op te slaan   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);

   }   alert("Following info was copied to your clipboard:nn" + meintext);   return false;}//--></script>      

 

to call that script out of your HTML use e.g. this code:

<span onclick='return copy_clip("this is your personal text")'>

test this script:

click somewhere on this line to copy some text to your clipboard (you should have the text “Hello World, from www.krikkit.net!” in your clipboard afterwards)

 

notes about security:

a cause of the tight security settings in mozilla you have to sign the javascript to make it work another way is to change your firefox/mozilla settings

to do this add this line to your prefs.js file in your firefox/mozilla user profile directory

user_pref("signed.applets.codebase_principal_support", true);

or change it from within the browser with calling the “about:config” page

 

 

huge parts of the script are directly copied from the following sources:

http://www.xulplanet.com/tutorials/xultu/clipboard.html
http://www.codebase.nl/index.php/command/viewcode/id/174

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>