Home > Article > Web Front-end > JS compatible with mainstream browsers to copy content to the clipboard_javascript skills
Nowadays, there are more and more types of browsers, such as IE, Firefox, Chrome, Safari, etc., so it is not that easy to implement a small function of copying content to the clipboard in js.
In the era of FLASH 9, there was a solution to kill all browsers’ js and copy content to the clipboard :
This solution is one of the most popular methods: The famous Clipboard Copy solution uses a clipboard.swf as a bridge to copy content to the clipboard.
The principle is: create a hidden flash file, and assign "clipboard=.." to the variable FlashVars in flash. Through this assignment, flash will put the copied content on the clipboard. This method is compatible with IE, Firefox, Opera, chrome, and Safari. It is truly a “universal” solution. The installation rate of browser Flash is very high, which is almost a perfect solution.
Download address of clipboard.swf: http://www.jeffothy.com/weblog/uploads/clipboard.php
But in the Flash 10 era, the above method no longer works.
Because flash10 stipulates that the clipboard can only be accessed by performing real operations on the swf (such as mouse clicks), and the above method only uses a hidden swf file to operate the flash clipboard through javascript, and the user does not There is no actual operation on the swf file, so this method is invalid.
So how to solve this "real operation" problem? You can use a JavaScript library: Zero Clipboard. This js library can support copying to the clipboard using flash 10. The principle of this method is to cover a DOM element such as button or div on a transparent flash (invisible to the user). When the DOM is clicked, the flash is actually clicked, thereby accessing the flash clipboard.
The following is a debugged example:
点击下载该类库: http://www.jb51.net/jiaoben/24961.html
调试时请上传到网站,本地直接打开flash会出错的,没权限。zeroClipboard.js文件里moviePath属性是falsh的地址,就是目录下的那个ZeroClipboard.swf存放的地址位置。
这种js复制内容到剪贴板的方案可支持浏览器:Firefox / IE / opera / chorme / safari 所有浏览器!