The Clipboard solution cannot be accessed when JavaScript is implemented in the Chrome browser clipboard

Source: Internet
Author: User

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

Related Article

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.