最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using document.execCommand('copy') in mobile - Stack Overflow

programmeradmin0浏览0评论

Is there a way to copy to mobile clipboard? I've been researching for days but haven't found a good solution. Clipboard.js doesn't seem to work on mobile, giving me an error "no support :("

I'm currently using the following function:

function copytext(text) {
    var textField = document.createElement('textarea');
    textField.innerText = text;
    document.body.appendChild(textField);
    textField.select();
    document.execCommand('copy');
    textField.remove();
}

Works like a charm on chrome on my desktop. But on chrome mobile, nothing gets copied.

Is there a solution out there?

Is there a way to copy to mobile clipboard? I've been researching for days but haven't found a good solution. Clipboard.js doesn't seem to work on mobile, giving me an error "no support :("

I'm currently using the following function:

function copytext(text) {
    var textField = document.createElement('textarea');
    textField.innerText = text;
    document.body.appendChild(textField);
    textField.select();
    document.execCommand('copy');
    textField.remove();
}

Works like a charm on chrome on my desktop. But on chrome mobile, nothing gets copied.

Is there a solution out there?

Share edited Mar 31, 2016 at 18:18 Zeno Rocha 3,5961 gold badge25 silver badges27 bronze badges asked Mar 14, 2016 at 19:33 Jackson CunninghamJackson Cunningham 5,0933 gold badges35 silver badges84 bronze badges 5
  • 1 What version of Chrome and what mobile device? It's supposedly available in mobile Chrome 42+ and mobile Firefox 41+, presumably both Android. – Alexander O'Mara Commented Mar 14, 2016 at 19:37
  • Weird. I'm on the most recent version. Here's a live page with the copy link button: trailerpuppy./trailers/… – Jackson Cunningham Commented Mar 14, 2016 at 19:40
  • 1 Working on my end, Chrome 49.0.* for Android. I'm guessing you're using iOS Chrome? – Alexander O'Mara Commented Mar 14, 2016 at 19:44
  • Ah.. yeah I think my version may be outdated. It was the most recent version available on my iOS version.. But I need to update iOS. Thanks for troubleshooting. – Jackson Cunningham Commented Mar 14, 2016 at 19:45
  • Chrome for iOS is basically just an alternative app for the same rendering and JavaScript API as the built-in WebKit for Safari. Unless iOS Safari supports it, iOS Chrome and Firefox probably won't either. – Alexander O'Mara Commented Mar 14, 2016 at 19:47
Add a ment  | 

1 Answer 1

Reset to default 4

According to MDN, document.execCommand('copy') is available in the following mobile browsers:

  • Chrome for Android 42+
  • Firefox Mobile (Gecko) 41+

Note that this does not include the iOS Chrome or Firefox, which per-Apple's requirement, both must use the iOS supplied WebKit. Until iOS Safari supports it, iOS Chrome and iOS Firefox probably cannot.

Update:

Safari on iOS 10+ supports cut and copy

发布评论

评论列表(0)

  1. 暂无评论