So I'm trying to redirect mobile pages on my website. If someone is on a mobile device then it would send them to the mobile site rather than the desktop site. It works on Chrome. However, it is not working on macOS Safari. Any idea why? The JavaScript is below:
<script>
if (screen.width <= 420) {
document.location="mobileindex.html";
}
</script>
So I'm trying to redirect mobile pages on my website. If someone is on a mobile device then it would send them to the mobile site rather than the desktop site. It works on Chrome. However, it is not working on macOS Safari. Any idea why? The JavaScript is below:
<script>
if (screen.width <= 420) {
document.location="mobileindex.html";
}
</script>
Share
Improve this question
edited Mar 2, 2018 at 21:55
toastrackengima
8,7624 gold badges53 silver badges62 bronze badges
asked Mar 2, 2018 at 21:38
Gib CannonGib Cannon
531 silver badge7 bronze badges
1 Answer
Reset to default 10screen.width
returns the width of the screen, not the browser window. This is good as it means that we can't falsely redirect users to the mobile site.
In Chrome, we can test that our logic works using the Device Emulator. When a page is loaded in the Device Emulator, screen.width
returns the width of the device being emulated, rather than the host puter's screen.
However, in Safari, the corresponding Responsive Design Mode does not work this way. screen.width
will always return the width of the puter monitor in Safari, not the width of the emulated device.
In other words, this is a problem with Safari's implementation, not your code. It will work fine on an actual mobile device.