How to detect copy to clipboard function before using it

I am trying to create a copy function in pure JS, so there is no flash. The problem is that I do not want to show the copy button when the browser does not support copying to the clipboard.

I use the document.execCommand('copy') method to copy to the clipboard, but support for this is not the best. For example, safari has an execCommand function, but does not support the copy option. This means that I just can't just check if the function exists.

Due to this ingenious support, I think I will have to go along the path of discovering the browser, just like the github I came across when looking at the zeroclipboard . Here is the implementation I found.

Is there a correct way to detect a user agent? I would prefer not to use NavigatorID.userAgent as this is deprecated according to MDN .

+7
javascript copy-paste
source share
1 answer

I noticed that in Safari prior to version 10 (tested on 9.0 and 9.1) the following construction

 document.execCommand('copy'); 

will return false . This fact can be used to check compatibility in Safari.

 if (false == document.execCommand('copy')) { // Logic for handling the copy functionality in some other way } 
+4
source share

All Articles