0

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");
}
  • Does this answer your question? [Detecting a mobile browser](https://stackoverflow.com/questions/11381673/detecting-a-mobile-browser) – Emiel Zuurbier Feb 13 '22 at 14:40

1 Answers1

0

Detecting a mobile browser

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

Colleen
  • 48
  • 7