0

My code looks okay and works fine. But when I force desktop site on chrome Android, it gets the function and executes the function supposed to work on only desktop or not mobile/tablet devices. Why the navigator.userAgent.match is working like desktop in mobile? Please help me.

JAVASCRIPT

  function upme()
  {
      var isdesk = !(navigator.userAgent.match(/iphone|ipod|android|blackberry|opera|mini|windows phone|palm|smartphone|mobile|phone|iemobile|ipad|webos|xoom|sch-i800|playbook|tablet|kindle/i));
  
  // event listeners
  window.addEventListener('resize', relod, false);
  function relod() 
  { 
     if(isdesk) 
     { 
         location.reload(); 
     } 
  }
  window.addEventListener('scroll', doso, false);
  window.addEventListener('resize', doso, false);
  function doso()
  {
    if(window.matchMedia("(min-width: 765px)").matches && isdesk)
    {
        document.getElementById("last-dab").style.visibility = "visible";
    }
  }
}

So the function should be working on desktop devices for the isdesk condition check. But when forcing desktop site option on chrome breaks everything by doing jittery behavior and the function gets done/executed in mobile, which should not happen. What is wrong?

Can I detect if the force desktop site is enabled or not using javascript. If so please provide a JavaScript solution.

Soumya Roy
  • 265
  • 3
  • 11
  • To show/hide content base on width/height of the view port user CSS media query https://www.emailonacid.com/blog/article/email-development/emailology_media_queries_demystified_min-width_and_max-width/ – Jan Pfeifer Jan 19 '23 at 12:43
  • That's not the case here. The reload is working on mobile while forcing desktop site. How to fix it so that mobile will not reload. Not enabling the desktop site option works fine. How to detect if a user is requesting desktop site view? – Soumya Roy Jan 19 '23 at 12:48
  • It depends HOW user is forcing view change. Your only option is probably store some information there (localStorage). – Jan Pfeifer Jan 19 '23 at 12:52
  • Someone said if a user comes straight in on a mobile requesting a desktop site, the device is classed as a desktop. Is it possible to set a cookie and alter the force desktop site enable/disable? If you know some cookie (localStorage) method, please provide some JavaScript. Thanks for your time. – Soumya Roy Jan 19 '23 at 12:56
  • https://stackoverflow.com/questions/35349122/how-to-know-if-a-mobile-device-is-showing-desktop – Jan Pfeifer Jan 19 '23 at 13:07
  • Thanks, I just found the link too. I don't wanna add heavy 23k library. They are saying to check the OS of the device. Is it gonna work? I think setting up cookie can solve this without loading the entire library just for this. – Soumya Roy Jan 19 '23 at 13:15
  • AFAIK there is not much more you can do. – Jan Pfeifer Jan 19 '23 at 13:21

0 Answers0