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
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.


Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Call a function within the ASP code 4 19
binding event to form in 9 36
Running a javascript project in WebStorm 2 25
Fix Form size HTML 16 13
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

861 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