Dmitry
Dmitry

Reputation: 14632

Copy&Paste doesn't work on Mobile Safari and CodeMirror

I have wrote the following code:

function copy() {
  if(window.clipboardData) {
    window.clipboardData.clearData();
    window.clipboardData.setData("Text", document.getElementById('txtacpy').value);
  } 
}

function paste() {
  if(window.clipboardData) {   
    document.getElementById('txtapaste').value = window.clipboardData.getData("Text");
  } 
}

When CodeMirror editor is focused I call:

paste();

But nothing happens! Browser is Mobile Safari. How to fix the issue?

Upvotes: 0

Views: 1448

Answers (1)

jfriend00
jfriend00

Reputation: 708026

This functionality is not supported in most browsers because of security issues without allowing a web page to have access to your clipboard. This is not only Mobile Safari. You won't find it supported in most browsers (like Chrome or Firefox).

Some people use a Flash work-around called zClip/ZeroClipboard that does allow copying to the clipboard, but only from a direct user click on the Flash object. This is obviously not an option in mobile safari.

If you are only trying to move data around within your page, then you don't have to use the system clipboard to do that - you can create your own holding area for the data (a javascript variable) and put the data there upon Copy and retrieve it from there upon Paste. Then, you use normal DOM functions (not copy/paste functions) to get the data from a field or to put the data into a field.

Upvotes: 2

Related Questions