Support for multiple browsers (IE, Firefox, Opera) clipboard copy function revision _javascript tips

Source: Internet
Author: User

Be aware that Firefox must be set under
Signed.applets.codebase_principal_support
in the Firefox browser address bar input About:config Filter input Signed.applets.codebase_principal_support Double-click set to True to support. Because Firefox by default does not support scripting operations on the Clipboard.
because browsers that are just not supported are the safest browsers.

<script> function CopyToClipboard (TXT) {if (window.clipboarddata) {window.clipboardData.clearData (); Window.clipboardData.setData ("Text", txt); Alert ("Copy succeeded!") "); 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 you from clipboard operation, open ' About:config ' to set Signed.applets.codebase_principal_support ' to True" Then try again, can refer to http://www.jb51.net/article/16705.htm "); 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); } copytoclipboard ("Http://www.jb51.net"); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


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.