8

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?

Zeno Rocha
  • 3,226
  • 1
  • 23
  • 27
Jackson Cunningham
  • 4,973
  • 3
  • 30
  • 80
  • 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 Mar 14 '16 at 19:37
  • Weird. I'm on the most recent version. Here's a live page with the copy link button: http://www.trailerpuppy.com/trailers/captain-america-civil-war-trailer-8c60bffa-6534-4496-bb89-e6ffc8684e2a – Jackson Cunningham Mar 14 '16 at 19:40
  • 1
    Working on my end, Chrome 49.0.* for Android. I'm guessing you're using iOS Chrome? – Alexander O'Mara Mar 14 '16 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 Mar 14 '16 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 Mar 14 '16 at 19:47

1 Answers1

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

dwjohnston
  • 11,163
  • 32
  • 99
  • 194
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171