I have written a function to copy text from a textarea but I need to have a particular code block executed if the user is using an apple mobile device.
I tried the following statement
if (navigator.userAgent.match(/iphone|ipod|ipad/)) {
// code
}
but it doesn't seem to work very well.
Do you have any suggestions?
/* Full code */
function copy(copyText) {
if (navigator.userAgent.match(/iphone|ipod|ipad/)) {
// handle iOS devices
copyText.contenteditable = true;
copyText.readonly = false;
var range = document.createRange();
range.selectNodeContents(input);
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
input.setSelectionRange(0, 999999);
} else {
// other devices are easy
copyText.select();
}
document.execCommand("copy");
}
Not sure if this is what youre looking for but it looks like might have the answer to your question?
Using navigator.userAgentData You may also use navigator.userAgentData.mobile, but userAgentData is still experimental, so it is not recommended for use in production.
const isMobile = navigator.userAgentData.mobile;
That part might be of use