Copy the js Code of the Input content _ support all browsers, and correct the problem above Firefox3.5 _ javascript skills

Source: Internet
Author: User
Today, I made a function with an input containing content. click the button to copy the content to the clipboard.
However, IE6 can use two lines of JS Code, and IE7 and above are similar, but a security prompt is disgusting. If you see it, you must be skeptical;
But Firefox and Chrome won't let you copy them;
I remember that I had this code on the Internet. I found that I could not find it in firefox3.5 or later versions. I finally found one. The code is quite complex and not very dare to use it.
Finally, there was no way to check the information and I wrote one:
The main points are as follows:
1. For security reasons, Firefox does not allow replication;
2. In Flash, you can use System. setClipboard () to throw the content to the clipboard and then let FLASH work in Firefox;
3. After FLASH Player 10.0, the content of System. setClipboard must be in FLASH;
4. Use ExternalInterface to communicate with JS;
5. ExternalInterface must be referenced in flash8;
Default HTML code:

The Code is as follows:




Copy



Therefore, when designing, make the first judgment. If it is IE, use the default code. If it is not, overwrite the default button with a FLASH;

The Code is as follows:


If (window. XMLHttpRequest) {// if it is not IE, use FLASH to copy
$ ('Buttonbox'). innerHTML ='Height = "23" quality = "high"
Pluginspage = "http://www.macromedia.com/go/getflashplayer"
Type = "application/x-shockwave-flash">';
}


Below are all JS files:

The Code is as follows:


Script
Function $ (id ){
Return document. getElementById (id );
}
Function copy () {// ie6
Var value = $ ('testinput'). value;
Window. clipboardData. clearData ();
Window. clipboardData. setData ("Text", value );
Alert ('copied successfully! ');
}
Function flashCopy () {// firefox .......
Return $ ('testinput'). value;
}
Function flashCopyBack (){
Alert ('copied successfully! ');
}
If ("v "! = "V") {// if it is not IE, use FLASH to copy
$ ('Buttonbox'). innerHTML ='';
}
Script


The FLASH button code is as follows:

The Code is as follows:


On (release ){
Import flash. external. ExternalInterface;
Var inputText = ExternalInterface. call ('flashcopy ');
System. setClipboard (inputText );
ExternalInterface. call ('flashcopyback ');
// _ Root. boboText. text = inputText;
}


The principle is to avoid the security restriction. When you click the button in FLASH, you can use the code in FLASH to call the JS Code in the page. The JS Code can get the value in INPUT and then pass it to FLASH, at this time, FLASH has these values, and then FLASH itself through the System. setClipboard saves these values to the clipboard. Then, he calls flashCopyBack on the page and flashCopyBack only performs one thing, indicating that the copy is successful!
I have tested it in Firefox, chrome, and ie. No problem. If anyone finds any problem, please let me know. Thank you, because I have started using it now!
Reprinted please indicate the source: Zombies

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.