Copying text (NOT WITHIN A FORM FIELD) to the clipboard


I am after a javascript function which will work as follows and will copy the parsed text into the windows clipboard:

onclick="copy_to_clipboard('text to copy goes here')"

I dont want to involve form fields.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

one of our experts have written this:

Use htis
<script language="javascript" type="text/javascript">
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:'UniversalXPConnect');
   // maak een interface naar het clipboard
  var clip = Components.classes[';1'].createInstance(Components.interfaces.nsIClipboard);
  if (!clip) return;
   // maak een transferable
  var trans = Components.classes[';1'].createInstance(Components.interfaces.nsITransferable);
  if (!trans) return;
   // specificeer wat voor soort data we op willen halen; text in dit geval
   // 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[";1"].createInstance(Components.interfaces.nsISupportsString);
   var copytext=meintext;;
   var clipid=Components.interfaces.nsIClipboard;
   if (!clip) return false;
  alert("Following info was copied to your clipboard:\n\n" + meintext);
  return false;

<body onload=copy_clip("partha")>



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I'm quite sure that you stuck with the form fields, this is an good example.
Hi there.

do you need a script that can copy to clipboard anything you have selected on the page?
even if its a static text anywhere in the page?
or only the way you described it (that you already have the text).

any of the aboove can be done, just explain youself.


Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

hi there.

This was tested on IE only but its ready for NS as well.
I think this is what you need.
Simply select any text (i can do for image as well if you need it)
and you will see the data in the clipboard after clicking on the button.

if you want to check and see thats its realy the text simple paste it any where
in any application on your machine.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">



//      this will call the right function based no the browser
function copySelection(){

       if (document.getSelection)
            return NSSelection();

      if ( document.selection )
            return IESelection();

//      first get the selected (marked ) text in the document./
//      it can be any text from any part of the page (field) or
//      just any other text.

function NSSelection(){
      //for earlier version of NS use:
      //var theSelection = document.getSelection();
      //from Newer versions use:
      var theSelection = window.getSelection();
    if (!theSelection)
          return false;
      //      missing NS implementation .
      //      i will fill it here if you need it as well

function IESelection() {
      var theSelection = document.selection.createRange().text;
         if (!theSelection)
          return false;

      window.clipboardData.setData( 'Text' , theSelection);
    document.getElementById('clipboardData').innerText = IEgetClipboardData();

function IEgetClipboardData(){
      return window.clipboardData.getData( 'Text');

<div>This is the text are to test.</div><br>
<span>Another line</span><br>
<textarea rows=10 cols="10">Text area text goes here</textarea>
<input type='button' onclick='copySelection()' value='Copy Data To clipboard'>
<b>ClipBoard Data:</b><br><span id='clipboardData'></span>

rfilaAuthor Commented:

Nice and easy:        window.clipboardData.setData("Text", data);
where data = the text to copy to the clipboard.

rfilaAuthor Commented:
Perhaps I should mention I have only need this to work in IE and therefore havent bothered with the complicated netscape script.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.