How to detect copy to clipboard functionality before using it
I'm trying to create a copy function in pure JS, so no flash. The problem I've got is that I don't want to show the copy button when the browser doesn't support copying to clipboard.
I'm using the document.execCommand('copy')
method to do the copying to clipboard but the support for this isn't the best. For example, safari has the execCommand function but doesn't support the copy parameter. This means that I can't simply just check if the function exists.
Because of this dodgy support I think I'm going to have to go in the way of browser detection, just like github does which I came across from looking at a zeroclipboard issue. Here is the implementation I found.
Is there a correct way to detect the user agent? I'd rather not use NavigatorID.userAgent as that is deprecated according to MDN.
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 for checking compatibility in Safari.
if (false == document.execCommand('copy')) {
// Logic for handling the copy functionality in some other way
}
链接地址: http://www.djcxy.com/p/90776.html
上一篇: 如何在通用Windows类库中使用最新的网点核心?
下一篇: 如何在使用前检测复制到剪贴板的功能