Javascript code copying skills compatible with IE and Firefox

Source: Internet
Author: User
This is a copy-and-click code. Currently, there is not only one link on my page that requires this code. Please kindly advise me How to rewrite this JavaScript code by ID, so that one click Copy corresponds to one corresponding content.

<Script type = "text/javascript"> function copyCode (id) {var testCode = document. getElementById (id). value; if (copy2Clipboard (testCode )! = False) {alert ("the generated code has been copied to the clipboard. You can use Ctrl + V to paste it to the desired place! ") ;}} 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 limit you to perform clipboard operations. Open 'about: config' and set signed. applets. codebase_principal_support 'is set to true and then try again. The relative path is the firefox root directory/greprefs/all. js? 1.1.9 "); 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/transferable1_1'assist.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-string1_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);} script <p> [Click to copy 1] <textarea rows = 2 id = "txtTestCode1" style = "width: 500px; font-family: Verdana, arial; font-size: 12px; height: 55px; border: 1px solid #999999; "> copy content 1 </textarea> </p> <p> [Click to copy 2] <textarea rows = 2 id =" txtTestCode2 "style =" width: 500px; font-family: Verdana, Arial; font-size: 12px; height: 55px; border: 1px solid #999999; "> copy content 2 </textarea> </p> <p> [Click to copy 3] <textarea rows = 2 id =" txtTestCode3 "style =" width: 500px; font-family: Verdana, Arial; font-size: 12px; height: 55px; border: 1px solid #999999; "> copy content 3 </textarea> </p>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


The following content mainly applies textarea as the content carrier. If it is p and so on, innerText is required for direct webpage marking, but firefoxfirefox does not support innerText. Therefore, we need to consider how to make firefox support innerText, in this way, you can achieve it perfectly. For specific code, refer to the functional code of the script house.
The next article describes how to enable firefox to support the innerText implementation 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.