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

Posted on 2003-11-13
Last Modified: 2012-06-22

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.

Question by:rfila
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 14

Accepted Solution

p_partha earned 250 total points
ID: 9741929
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")>



Expert Comment

ID: 9741943
I'm quite sure that you stuck with the form fields, this is an good example.
LVL 10

Expert Comment

ID: 9741969
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.


Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

LVL 10

Expert Comment

ID: 9742200
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>


Author Comment

ID: 9746585

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


Author Comment

ID: 9746592
Perhaps I should mention I have only need this to work in IE and therefore havent bothered with the complicated netscape script.

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using, I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question