Sometimes, we want to detect if user is using web view for Android/iOS or a regular browser with JavaScript.
In this article, we’ll look at how to detect if user is using web view for Android/iOS or a regular browser with JavaScript.
How to detect if user is using web view for Android/iOS or a regular browser with JavaScript?
To detect if user is using web view for Android/iOS or a regular browser with JavaScript, we use the user agent string.
For instance, we write
const userAgent = window.navigator.userAgent.toLowerCase();
const safari = /safari/.test(userAgent);
const ios = /iphone|ipod|ipad/.test(userAgent);
if (ios) {
if (safari) {
//...
} else if (!safari) {
//...
}
} else {
//...
}
to get the user agent string with window.navigator.userAgent
.
We return a all lower case version it with toLowerCase
.
Then we check if the user if using Safari with /safari/.test(userAgent)
And we check if the user if using iOS with /iphone|ipod|ipad/.test(userAgent)
.
Conclusion
To detect if user is using web view for Android/iOS or a regular browser with JavaScript, we use the user agent string.