I do not want a user to be able to open my website on a mobile device (regardless of it being an iOS or Android device). The website is meant to be viewed only as a desktop application (on laptops, monitors or TVs) due to poor responsiveness on mobile devices. Is there any way I can warn a user (by showing a pop-up) when the website is opened on a mobile device?
Edit: Found a fix that is able to kind of gets the desired results.
window.addEventListener("resize", () => {
const width = window.innerWidth;
const height = window.innerHeight;
if (width<900 || height<600){
alert("This website does not support the current viewport.")
}
console.log(`The viewport's width is ${width} and the height is ${height}.`);
});
2
Answers
Yes, we can warn the user through JavaScript.
You can use the built-infunction in JS
navigator.userAgent.match(/device/i)
, e.g.If the snippet above returns true if the user uses an Android device to view the website.
For iOS it will be
/iPhone/i
and similar for other devices. By getting the values from the if statement you can render the popup.For more info, you can check out this article
.
Yes!
You can detect if the page is opened on mobile with this code what I found. This function returns true is the device is a mobile and false if not.
And you can show a pop-up like this: