Languages :: DHTML, JScript :: how to copy and paste an HTML control 's contents to the Clipboard ? |
|||
| By: Josdeveld |
Date: 03/05/2006 10:46:34 |
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 | 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 | 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)
|
|||
| By: VGR | Date: 03/05/2006 12:09:59 | 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 |
|||
| Add This Article To: | |||
| |
|
|
|
| |
|
|
|








