Paul Tanasă Paul Tanasă - 3 months ago 8
Javascript Question

Redirect to another page by width of browser window JavaScript

I run my code with xampp and when I access my html file (with Firefox), I am redirected even if my browser has more than 1400 on width.

This is my html code:

<html lang="en-us">

<script src="my.js"></script>

<h1> Hello, Paul! </h1>


This is my javascript code on "my.js" file:

if (screen.width <= 1400) {
window.location = "mobile";

I tried to clean the cookies, but doesn't work.

Any idea?


screen.width returns the width of your monitor, not the document.

There's document.width for the purpose.

if (document.width <= 1400) {
    window.location = "mobile";