Home > Software engineering >  Redirect only for desktop device
Redirect only for desktop device

Time:03-20

I am trying to redirect desktop device changing a script:

if (screen.width <= 699) {
  document.location = "Other URL";
}

699 to major width but doesn't work. Please how can do this redirect? My purpose is do it only for descktop but I have success only with mobile. Aby suggest?

CodePudding user response:

Here is script in ECMA6 [works on modern browsers version]. also redict page for desktop device.

var isMobile = navigator.userAgent.toLowerCase().indexOf('mobile') > -1;
var getChromeVersion = function() {
  var raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9] )\./);
  return raw ? parseInt(raw[2], 10) : false;
};


console.log('isMobile :'   isMobile)
console.log('getChromeVersion :'   getChromeVersion())

if (isMobile == false) {
// if device is desktop then redirect our page
 window.location.replace("https://maximumroulette.com");
 // OR
 // window.location.href = "https://www.google.com";
}

CodePudding user response:

Where is my mistake? I have placed links but redirection of descktop devices does not work. Please, can you tell me? Thank you very much

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento senza titolo</title>
<script>
 ...
 
...

    if (navMozilla && NOMOBILE == 1 && gecko && navFirefox) {
        HREFF = "https://www.google.com/";                       //  REDIRECT ADDED
        HREFTXT = "firefox_desktop";
    }
    if (navOpera && TYPEOFANDROID == 0 && !mobile) {
        HREFF = "#";                    
        HREFTXT = "opera_desktop";
    }

    //linux
    if (navUbuntu && navMozilla && navFirefox && navLinux) {
        HREFF = "https://www.google.com/";                      //  REDIRECT ADDED
        HREFTXT = "firefox_desktop_linux";
    }
    if (navMozilla && navLinux && navChrome && navSafari) {
        HREFF = "https://www.google.com/";                      //  REDIRECT ADDED
        HREFTXT = "chrome_desktop_linux";
    }
    if (navMozilla && navLinux && navChrome && navSafari && navOpera) {
        HREFF = "https://www.google.com/";                       //  REDIRECT ADDED
        HREFTXT = "opera_desktop_linux";
    }

    ...
    ...
</script>
</head>

<body>
Some text only for mobile device...
Some text only for mobile device...
Some text only for mobile device...
</body>
</html>


  • Related