Gib Cannon
Gib Cannon

Reputation: 53

Why does window.screen not work on macOS safari

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>

Upvotes: 5

Views: 3042

Answers (1)

toastrackengima
toastrackengima

Reputation: 8732

screen.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 computer's screen.

However, in Safari, the corresponding Responsive Design Mode does not work this way. screen.width will always return the width of the computer 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.

Upvotes: 10

Related Questions