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

Hi

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.

Thanks
Richard
rfilaAsked:
Who is Participating?
 
p_parthaCommented:
one of our experts have written this:

Use htis
<html>
<head>
<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:
  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:\n\n" + meintext);
  return false;
}
//-->
</script>      


<title>Untitled</title>
</head>
<body onload=copy_clip("partha")>

</body>
</html>


Partha
0
 
wfRGBCommented:
I'm quite sure that you stuck with the form fields, this is an good example.
http://www.htmlgoodies.com/beyond/clipboard.html
0
 
NushiCommented:
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.

Nushi.

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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

Nushi.



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

<html>
<head>
      <title>Untitled</title>
</head>

<body>
<script>

//      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');
      }
      
</script>

<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>
<br>
<br><br>
<input type='button' onclick='copySelection()' value='Copy Data To clipboard'>
<br><Br>
<b>ClipBoard Data:</b><br><span id='clipboardData'></span>

</body>
</html>
0
 
rfilaAuthor Commented:
Hi

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

Thanks
Richard
0
 
rfilaAuthor Commented:
Perhaps I should mention I have only need this to work in IE and therefore havent bothered with the complicated netscape script.
Ta
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.