Cross-browser development experience Summary (iv) How to write clipboard _javascript tips

Source: Internet
Author: User
IE, Firefox can support JavaScript to write content to the Clipboard
IE can easily support clipboard content writing commands, can use ExecCommand (), can also use the window.clipboarddata.

With execcommand, you need to select from the page what you want to copy to the Clipboard, such as the following code:
Copy Code code as follows:

var doc = Obj.createtextrange ();
Doc.select ();
Doc.execcommand (' copy ');

Using the Window.clipboarddata method, the code also implements Firefox's ability to write to the Clipboard:
Copy Code code as follows:

if (window.clipboarddata)//ie
{
Window.clipboardData.clearData ();
Window.clipboardData.setData ("Text", txt);
}
else if (window.netscape)
{
try {netscape.security.PrivilegeManager.enablePrivilege ("Universalxpconnect");
}
catch (E)
{
Alert ("Please visit ' about:config ' and set Signed.applets.codebase_principal_support as ' true ');
Prompts users for security settings for open browsers
}

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
Clip.setdata (Trans,null,clipid.kglobalclipboard);
}

The above code can be implemented in IE, Firefox to the Clipboard to write custom content, but opera and WebKit kernel browser More security requirements, do not support this JavaScript directly manipulate the contents of the Clipboard, only through other scripting language "curve Save the Nation."

Opera, Safari, Chrome, using ActionScript to write content to the Clipboard

The implementation of the original action button with flex or Flash to achieve its appearance, replace the original picture or Text button, and then click the button, execute the following ActionScript script:

Get what you need to write to the Clipboard from the browser environment

var s:string = String (Externalinterface.call ("Geturl4clipboard")); Geturl4clipboard is the JavaScript method for the return clipboard content on the page

Set Clipboard contents

System.setclipboard (s);

Invoke JavaScript functions that need to continue after the contents of the Clipboard have been set, such as prompting user information

Externalinterface.call ("copyurlcompleted"); Copyurlcompleted is the JavaScript method on the page and continues to perform the work after replication

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.