Categories
JavaScript Answers

How to detect if browser is running on an Android or iOS device with JavaScript?

Spread the love

Sometimes, we want to detect if browser is running on an Android or iOS device with JavaScript.

In this article, we’ll look at how to detect if browser is running on an Android or iOS device with JavaScript.

How to detect if browser is running on an Android or iOS device with JavaScript?

To detect if browser is running on an Android or iOS device with JavaScript, we check the user agent.

For instance, we write

const isMobile = {
  android() {
    return /Android/i.test(navigator.userAgent);
  },
  iOS() {
    return /iPhone|iPad|iPod/i.test(navigator.userAgent);
  },
};

to check if the page is loaded in a browser on Android with /Android/i.test(navigator.userAgent);.

And we check if the page is loaded in a browser on Android with /iPhone|iPad|iPod/i.test(navigator.userAgent).

navigator.userAgent has the user agent string.

Conclusion

To detect if browser is running on an Android or iOS device with JavaScript, we check the user agent.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *