Paul Tanasă Paul Tanasă - 4 months ago 10
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:

<!DOCTYPE HTML>
<html lang="en-us">

<head>
<title>Packet</title>
<script src="my.js"></script>
</head>

<body>
<h1> Hello, Paul! </h1>
</body>

</html>


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?

Answer

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";
}
Comments