I'm currently trying to write a JS function to provide a "copy link on button click". It works fine on Android and PC but when I try it on my iPad or iPhone I'm getting an error:
TypeError: Argument 1 ('refNode') to Range.selectNodeContents must be an instance of Node
I've build in a way to copy it on IOS devices too, because the normal copy command don't works:
function copyUrl(e) {
var tmp = jQuery("<input>");
jQuery("body").append(tmp.val(e));
if (navigator.userAgent.match(/ipad|ipod|iphone/i)) {
var editable = tmp.contentEditable;
var readOnly = tmp.readOnly;
tmp.contentEditable = true;
tmp.readOnly = false;
var range = document.createRange();
range.selectNodeContents(tmp);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
tmp.setSelectionRange(0, 999999);
tmp.contentEditable = editable;
tmp.readOnly = readOnly;
} else {
tmp.select();
}
document.execCommand("copy");
tmp.remove();
alert("Link copied successfully!")
}
div {
padding: 30px;
}
a {
border: 1px solid;
padding: 12px 10px;
cursor: pointer;
}
a:hover {
border-color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<a class="btn-floating" onclick="copyUrl('google.de')">Share</a>
</div>
What have I missed?