In the use of JavaScript to achieve a simple copy to the Clipboard function, will consider the browser compatibility, mainly in IE and Firefox, the two browser, basically other browsers do not worry too much, Chrome is the same, nothing wrong. But Chrome is good, and suddenly after one months, it can't be copied to the Clipboard. For a moment do not know what reason. Online check, there is a reason: security issues, the browser is forbidden to access the Clipboard by default.
From the code level, think of a variety of methods, to solve compatibility, it is useless, is not to solve the compatibility of chrome.
Paste the JavaScript a generic copy and paste method, referencing the Clipboard class library, and then to implement. Specifically, we can refer to
HTTPS://clipboardjs.com/
Clipboard.on ('Success', Function (e) {console.info ('Action:', e.action); Console.info ('Text:', E.text); Console.info ('Trigger:', E.trigger); E.clearselection ();}); Clipboard.on ('Error', Function (e) {console.error ('Action:', e.action); Console.error ('Trigger:', E.trigger);});
Later, think about it, the problem should not be the code level, and then from the browser default settings to find the problem.
There is a way to say that you have to use JavaScript + Flash to achieve copy and paste, to solve the compatibility, look, this method is very complex. So I started from flash.
Allows flash loading within the browser's default settings.
This solves the problem technically.
But when it comes to business people, you may not know that, and you can add a business friendly tip:
Clipboard.on ('success', function (e) {// Copy paste succeeded callback function. Code execution here, the description can be written to the local window.localStorage.setItem after the chrome setting succeeds // succeeded (" Chromeallowflash","true");}
Then, when a specific click is copied to the Clipboard, a previous judgment is added:
var Chromeallowflash = Window.localStorage.getItem ( " chromeallowflash " // fix a problem with chrome that prevents scripts from accessing the clipboard for security reasons if (navigator.userAgent.indexOf (" chrome ") >-1 &&! chromeallowflash) {Alert ( " " ); }
Above, for reference only.
The Clipboard solution cannot be accessed when JavaScript is implemented in the Chrome browser clipboard