Skip to content

Instantly share code, notes, and snippets.

@agazso
Last active August 16, 2019 14:02
Show Gist options
  • Save agazso/fe91114923ddce5fd8a68c8f7f814d2e to your computer and use it in GitHub Desktop.
Save agazso/fe91114923ddce5fd8a68c8f7f814d2e to your computer and use it in GitHub Desktop.
Proof-of-concept copy to clipboard that works on Safari 12 on iOS
<html>
<head>
<style>
#textarea {
visibility: hidden;
}
</style>
<script>
function iosCopyToClipboard(el) {
var oldContentEditable = el.contentEditable,
oldReadOnly = el.readOnly,
range = document.createRange();
el.contentEditable = true;
el.readOnly = false;
range.selectNodeContents(el);
var s = window.getSelection();
s.removeAllRanges();
s.addRange(range);
el.setSelectionRange(0, 999999); // A big number, to cover anything that could be inside the element.
el.contentEditable = oldContentEditable;
el.readOnly = oldReadOnly;
var ret = document.execCommand('copy');
console.log('hello', ret);
document.getElementById('output').innerText = '' + ret;
window.location = 'https://felfele.com/';
}
function magic() {
iosCopyToClipboard(document.getElementById('textarea'));
}
</script>
</head>
<body onload="">
<textarea id='textarea'>
link
</textarea>
<button onclick="magic()">Copy</button>
<div id='output'></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment