Support for IE and Firefox clipboard operation code _javascript tips

Source: Internet
Author: User
Copy Code code as follows:

<title> support for IE and Firefox clipboard code </title>
<script type= "Text/javascript" >
function Setcopy (_stxt) {
try{
if (window.clipboarddata) {
Window.clipboardData.setData ("Text", _stxt);
else if (window.netscape) {//firefox's complicated.
Netscape.security.PrivilegeManager.enablePrivilege (' Universalxpconnect ');
var clip = components.classes[' @mozilla. Org/widget/clipboard;1 '].createinstance ( Components.interfaces.nsIClipboard);
if (!clip) return;
var trans = components.classes[' @mozilla. Org/widget/transferable;1 '].createinstance ( Components.interfaces.nsITransferable);
if (!trans) return;
Trans.adddataflavor (' Text/unicode ');
var str = new Object ();
var len = new Object ();
var str = components.classes["@mozilla. Org/supports-string;1"].createinstance ( Components.interfaces.nsISupportsString);
var copytext = _stxt;
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);
}
}catch (e) {}
}
</script>
<body>
<input type= "button" onclick= "setcopy" (' Hello '); "value= copy"/>
<div>
Firefox/3.0.14 can run the <br/>
Invalid <br/> under firefox/3.5.3
But consider this one more.
</div>
</body>


Here's how IE Firefox reads the information in the Clipboard
Copy Code code as follows:

function Getclipboard ()
{
if (Window.clipboarddata)
{
Return (Window.clipboardData.getData (' text '));
}
Else
{
if (Window.netscape)
{
Try
{
Netscape.security.PrivilegeManager.enablePrivilege ("Universalxpconnect");
var clip = components.classes["@mozilla. Org/widget/clipboard;1"].createinstance ( Components.interfaces.nsIClipboard);
if (!clip)
{
Return
}
var trans = components.classes["@mozilla. Org/widget/transferable;1"].createinstance ( Components.interfaces.nsITransferable);
if (!trans)
{
Return
}
Trans.adddataflavor ("Text/unicode");
Clip.getdata (trans, clip.kglobalclipboard);
var str = new Object ();
var len = new Object ();
Trans.gettransferdata ("Text/unicode", str, len);
}
catch (E)
{
Alert ("Your Firefox security restrictions restrict your clipboard operations, please open ' about:config ' to Signed.applets.codebase_principal_support ' set to True ') and try again," The relative path is the Firefox root directory/greprefs/all.js ");
return null;
}
if (str)
{
if (Components.interfaces.nsISupportsWString)
{
str = str.value.QueryInterface (Components.interfaces.nsISupportsWString);
}
Else
{
if (Components.interfaces.nsISupportsString)
{
str = str.value.QueryInterface (Components.interfaces.nsISupportsString);
}
Else
{
str = NULL;
}
}
}
if (str)
{
Return (str.data.substring (0, LEN.VALUE/2));
}
}
}
return null;
}

Here is the code to write the Clipboard under Ie,firefox
Copy Code code as follows:

Copy2clipboard=function (TXT) {
if (window.clipboarddata) {
Window.clipboardData.clearData ();
Window.clipboardData.setData ("Text", txt);
}
else if (navigator.userAgent.indexOf ("Opera")!=-1) {
Window.location=txt;
}
else if (Window.netscape) {
try{
Netscape.security.PrivilegeManager.enablePrivilege ("Universalxpconnect");
}
catch (e) {
Alert ("Your Firefox security restrictions restrict your clipboard operations, please open ' about:config ' to Signed.applets.codebase_principal_support ' set to True ') and try again," The relative path is the Firefox root directory/greprefs/all.js ");
return false;
}
var clip=components.classes[' @mozilla. Org/widget/clipboard;1 '].createinstance ( Components.interfaces.nsIClipboard);
if (!clip) return;
var trans=components.classes[' @mozilla. Org/widget/transferable;1 '].createinstance ( Components.interfaces.nsITransferable);
if (!trans) return;
Trans.adddataflavor (' Text/unicode ');
var str=new Object ();
var len=new Object ();
var str=components.classes["@mozilla. Org/supports-string;1"].createinstance ( Components.interfaces.nsISupportsString);
var Copytext=txt;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);
}
}

Specific applications, you can refer to the cloud-Habitat Community Code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.