admin管理员组

文章数量:1023815

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
Add a ment  | 

1 Answer 1

Reset to default 5

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 patibility in Safari.

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

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
Add a ment  | 

1 Answer 1

Reset to default 5

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 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