admin管理员组文章数量:1432213
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'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.
Share Improve this question asked Mar 22, 2016 at 12:01 silverlight513silverlight513 5,6754 gold badges28 silver badges38 bronze badges 3-
did you try
typeof document.execCommand !== 'undefined'
? – Zamboney Commented Mar 22, 2016 at 12:14 - 1 As I said in the question, safari has the function document.execCommand but doesn't support the parameter 'copy'. That's why I'm thinking of going down the route of browser detection. It also doesn't throw an error when trying to use the function with that param. – silverlight513 Commented Mar 22, 2016 at 12:25
- It looks to me like there's no reliable way other than user agent sniffing. But (and I realize it's 5 years later!) support for this is now ubiquitous; nothing that I can find released later than mid-2016 lacks support, so in my opinion detecting support is no longer necessary. – tremby Commented May 26, 2021 at 22:06
1 Answer
Reset to default 5I 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 patibility in Safari.
if (false == document.execCommand('copy')) {
// Logic for handling the copy functionality in some other way
}
本文标签: javascriptHow to detect copy to clipboard functionality before using itStack Overflow
版权声明:本文标题:javascript - How to detect copy to clipboard functionality before using it - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745578241a2664463.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论