简体   繁体   中英

URL Redirect Based on Screen Width

What I'm looking for is quite simply, a way to automatically redirect the user to a different URL when the screen or window width is less then 950px.

I don't want to use "refresh" as it's not recommended, and I don't do want to use "User Agent" either as it seams to me less reliable in the long term and I don't want to be concerned with updating this.

This is the Script I see suggested everywhere for this purpose but for some reason it doesn't do anything:

<script type="text/javascript">
  <!--
  if (screen.width <= 950) {
    window.location = "https://www.google.com/";
  }
  //-->
</script>

I've also tried it with all this variants with no success:

window.location
document.location
window.location.href
document.location.href

I've also tried placing it as the first thing after Head tag and even before Doctype. Nothing happens...

If you have to use Javascript, try the following:

<script type="text/javascript">
   function redirect() {
   if (screen.width <= 950) {
      window.location = "https://www.google.com/";
   }

and in your body add :

<body onload="setTimeout('redirect()', 300)">

This will redirect the user after 300 ms when the page is loaded, this way you are making sure that the width of the screen is available.

You can better use JQUERY for this...

// Returns width of browser viewport
$( window ).width();

See: http://api.jquery.com/width/

Example:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
  if($(window).width() <= 950) {
window.location = "https://www.google.com/";
}
});
</script>
</head>
<body>
<p>Website</p>
</body>
</html>

I'm answering my own question as I have recently found a more satisfying solution then the one provided by @Finduilas.

This solution not only redirects you in normal circumstances, it will also redirect you back and forth as you resize your window.

Even better! It will redirect you back and forth in mobile devices as you switch from landscape to portrait and vice-versa.

<script type="text/javascript">
    $(window).on('load resize',function(){
        if($(window).width() < 950){
            window.location = "https://www.google.com"
        }
    });
</script>
<script type="text/javascript">
  <!--
  if (screen.width <= 950) {
    window.location = "https://www.google.com/";
  }
  //-->
</script>

<!-- you forgot to add window.location.replace("https://www.google.com/"); and yours is a little wrong-->
<script type="text/javascript">
  <!--
  if (screen.width <= 950) {
    window.location.replace("https://www.google.com/");
  }
  //-->
</script>
USE THIS ON TOP OF THE HEAD TAG
<script >
  window.addEventListener('resize', function() {
    if (window.innerWidth <= "Your desired screen width") {
        window.location.href = "redirect loacation"; 
    }
});
</script>
</head>
<body > 

None of these scripts work. I have tried them all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM