visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: DHTML, JScript :: how to copy and paste an HTML control 's contents to the Clipboard ?


By: Josdeveld Belgium  Date: 03/05/2006 10:46:34  English  Points: 20 Status: Answered
Quality : Excellent
how to copy and paste an HTML control 's contents to the Clipboard ?

I want to get a textarea's contents to clipboard and be able to paste it in an other one using BUTTONs

can I do that ?
By: VGR Date: 03/05/2006 10:54:18 English  Type : Comment
Well, you can use the ugly execCommand() solution that works only for Internet Exploder 4.0+ , as explained on this page but I would recommend a more standard solution usign W3C DOM.

In short, solution Ugly#1 is to define an control with an ID (containing the text you want to copy) ; here it is a SPAN ; tou've a TEXTAREA ; it doesn'treally matter what. Then to define a hidden copy-holder on which you'll select, then to execute a quasi "shell command" named "Copy". It's ugly, I had warned you :D

HTML :

<SPAN ID="copytext"> This text will be copied onto the clipboard when you click the button below. Try it! </SPAN> <TEXTAREA ID="holdtext" STYLE="display:none;"> </TEXTAREA> <BUTTON onClick="ClipBoard();">Copy to Clipboard</BUTTON>



JScript :

<SCRIPT LANGUAGE="JavaScript" type="text/javascript"> function ClipBoard() { holdtext.innerText = copytext.innerText; Copied = holdtext.createTextRange(); Copied.execCommand("Copy"); } </SCRIPT>



Solution Better#2 I have to retrieve from my own code. Give me some minutes please.
By: VGR Date: 03/05/2006 11:29:41 English  Type : Comment
Well, a contrario with Internet Explorer which doesn't care if malicious Javascript code can steal your desktop's clipboard's contents and send it to anybody via HTTP, Mozilla is taking care of the issue (see one example of security risk) and by default there is NO ACCESS from Firefox/Mozilla/Gecko to the clipboard.

You ***may*** change your preferences to allow this access. You may even allow it for all sites. If you do so, you take a grand risk :D

Given you allowed access to the clipboard for specific (ar all) sites, you may then use some javascript code to use it. I will post this later.

For the moment, here's the action course to enable Firefox/Mozilla access to the Clipboard : (from infogears site)


* Close all Firefox/Mozilla browser windows.
* Navigate to your Firefox profiles directory. In Windows XP it will be something like:
C:\Documents and Settings\<XP Username>\Application Data\Mozilla\Firefox\Profiles\(8 random letters here).default
(You may need to turn off hidden system files for Windows XP to show this directory.)
* Open prefs.js in a text editor like notepad. (You can also edit/create user.js and use that file instead.)
* Copy/Paste the data between the two lines below into the file.
//-----------------------------------------------------------
user_pref("capability.policy.policynames", "allowclipboard");
user_pref("capability.policy.allowclipboard.Clipboard.cutcopy", "allAccess");
user_pref("capability.policy.allowclipboard.Clipboard.paste", "allAccess");
user_pref("capability.policy.allowclipboard.sites", "http://www.mysite.com https://www.mysite.com");
//-----------------------------------------------------------
* Substitute www.mysite.com with your domain name. NOTE: do not include an ending slash on the website, or copy/paste will not work. Also note that you can add multiple sites using spaces.
* Save/Close The File
* Restart your browser. Copy/Paste should now work.

How Do You Make Cut/Copy/Paste Work On All Sites?
This is not recommended, as you are opening yourself to security problems by trusting all sites. If you ever have sensitive information in your clipboard (ie. you copy phone numbers, credit card numbers, passwords, etc), then using this is not a good idea. With that said, here's how to do it:

* Instead of copy/pasting the lines in the above instructions you only need to copy/paste this instead:
//-----------------------------------------------------------
user_pref("capability.policy.default.Clipboard.cutcopy","allAccess");
user_pref("capability.policy.default.Clipboard.paste", "allAccess");
//-----------------------------------------------------------

By: VGR Date: 03/05/2006 12:09:59 English  Type : Answer
OK. In fact, I found out my old code doesn't work any more with Firefox. I got an "access denied" exception in the JS console.

That's it :

netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); //VGR REM if you get "Error: uncaught exception: A script from "http://***" was denied UniversalXPConnect privileges.", try to modify your prefs.js in your profile // create interface to the clipboard var clip = Components.classes['@mozilla.org/widget/clipboard;[[[[1]]]]'].createInstance(Components.interfaces.nsIClipboard); if (!clip) return; // create a transferable var trans = Components.classes['@mozilla.org/widget/transferable;[[[[1]]]]'].createInstance(Components.interfaces.nsITransferable); if (!trans) return; // specify the data we wish to handle. Plaintext in this case. trans.addDataFlavor('text/unicode'); // To get the data from the transferable we need two new objects 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);



After having read this MozillaZine entry about the Clipboard, I understand better why.

I also found out that the code I was using seems to be quite the same as the one presented on this XUL fr page. I also read that ONLY XUL has the privilege to access those interfaces, hence my Error: uncaught exception: Permission denied to get property UnnamedClass.classes error...

I must state that there is a Firefox extension named "clipboard helper" or the like.

BUT I think now that the best solution for you is to simulate the clipboard. Do NOT really try to use the cliboard. If you want to transfer data on a page from field to field, use a hidden TEXTAREA as a copy-holder, as explained above.

Best regards

Do register to be able to answer

EContact
browser fav
page generated in 96.957920 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page