Answer the question
In order to leave comments, you need to log in
How to solve execComand('copy') problem on IOS?
The essence of the problem: when you click on a button on the site, some information that is located somewhere on the site should be copied, in my case in `data-attr`. Copying everywhere except `IOS` can be easily done with `execCommand('copy')`. That is, we create some kind of hidden `input` or `textarea`, execute `select()` in it, and then `execCommand('copy')`. With IOS, everything is not obvious. Stackoverflow offers a solution via userAgent
const iosFlag = !!navigator.userAgent.match(/ipad|ipod|iphone/i);
const copyClipBoard() {
if (iosFlag) {
const oldContentEditable = this.textArea.contentEditable;
const oldReadOnly = this.textArea.readOnly;
const range = document.createRange();
this.textArea.contenteditable = true;
this.textArea.readonly = false;
range.selectNodeContents(this.textArea);
const s = window.getSelection();
s.removeAllRanges();
s.addRange(range);
this.textArea.setSelectionRange(0, 999999);
this.textArea.contentEditable = oldContentEditable;
this.textArea.readOnly = oldReadOnly;
document.execCommand('copy');
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question