Solved

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

Posted on 2003-11-13
6
584 Views
Last Modified: 2012-06-22
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
0
Comment
Question by:rfila
6 Comments
 
LVL 14

Accepted Solution

by:
p_partha earned 250 total points
ID: 9741929
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
 
LVL 5

Expert Comment

by:wfRGB
ID: 9741943
I'm quite sure that you stuck with the form fields, this is an good example.
http://www.htmlgoodies.com/beyond/clipboard.html
0
 
LVL 10

Expert Comment

by:Nushi
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.

Nushi.

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 10

Expert Comment

by:Nushi
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.

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
 

Author Comment

by:rfila
ID: 9746585
Hi

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

Thanks
Richard
0
 

Author Comment

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now