0

Its working when i use the portrait mode, but whit the landscape its not working. It returns false. it must return true, could some one help me ? thanks.

    function ismobile(){

    let mobileMode;
    let windowWidth = window.innerWidth;
    let windowHeight = window.innerHeight;

    if (windowWidth <= 375 && windowHeight <= 667 || windowWidth <= 675 && windowHeight <= 375 ) {

        mobileMode = true;

    }else{

        mobileMode = false;

    }   

    console.log("mobileMode " + mobileMode);

}
Hugo Seleiro
  • 2,467
  • 5
  • 26
  • 39
  • 2
    You're not returning anything. – Mr Lister Sep 21 '17 at 16:48
  • 1
    This would work better with CSS Media queries. –  Sep 21 '17 at 16:49
  • How im not returning ? if i can see in portrait true ? and landscape false ? i call ismobile(); function above my code and in a window.onresize function. – Hugo Seleiro Sep 21 '17 at 16:50
  • 1
    @HugoSeleiro returning a value requires you actually *use* the `return` keyword somewhere in the function. I don't see you returning anything, either. –  Sep 21 '17 at 16:51
  • Can you give an example of how you are actually using the ismobile function? – Mr Lister Sep 21 '17 at 16:53
  • window.onresize = function() { console.log("resize event"); ismobile(); } – Hugo Seleiro Sep 21 '17 at 16:53
  • and i call ismobile(); on top of my js , i only want true or false – Hugo Seleiro Sep 21 '17 at 16:54
  • I see, you're not testing its return value. Sorry, but the title to your question threw me off! Now you're saying that it writes "true" to the console, but if you rotate the phone, it writes "false"? – Mr Lister Sep 21 '17 at 16:55
  • You are using different values for the size of the screen (675 and 667). Is that by design? – Mr Lister Sep 21 '17 at 16:57
  • The objective is to see if im in a mobile mode, to then use that variable to make another conditions, what is happening is that when i rotate the device, it goes false...i already put my variables outside of my function scope. see how it goes, im at work, i have to leave, thanks for all the help – Hugo Seleiro Sep 21 '17 at 17:02

3 Answers3

1

I think you want to group the and statements together like this:

if ((windowWidth <= 375 && windowHeight <= 667) || (windowWidth <= 675 && windowHeight <= 375) ) {

        mobileMode = true;

    }else{

        mobileMode = false;

    }   
Pslice
  • 54
  • 2
  • 7
  • 1
    That would make it more readable, but it won't make a difference for the outcome. – Mr Lister Sep 21 '17 at 16:58
  • @MrLister, I think you are right, this doesn't work any differently and that follows with the [MDN web docs description on logical operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators) – Pslice Sep 21 '17 at 18:23
1

Try using

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera 
Mini/i.test(navigator.userAgent) ) {
     return true;
}
return false;

if you are trying to check if mobile, if you are just trying to check the size, then just return mobileMode, and group the statements like in @Patrick Cool's answer.

kcode
  • 352
  • 1
  • 2
  • 14
  • My objective is to have 2 menus, one for desktop , tablet and landscape mobile view, and another menu for the portrait mobile view !!! So right now im making the variables, true or false if im on that modes , to make the conditionals !! – Hugo Seleiro Sep 22 '17 at 07:52
1

If you want to detect if the website is in a mobile browser just don't check for window width and height since latest mobile phones have high resolutions. You can do a simple trick like below using touch events. This method does not depends on the screen orientation.

This is the best approach since it does not depends on browser types.

(function ismobile() {

  let mobileMode;

  if (typeof window.ontouchstart !== 'undefined') {
    //Mobile, portrait or landscape
    mobileMode = true;
  } else {
    //Desktop
    mobileMode = false;
  }

  console.log("Mobile mode: " + mobileMode);

})();
Thusitha
  • 3,393
  • 3
  • 21
  • 33
  • Thanks, for the help !! My objective is to see if im in a mobile mode, if so, i need to see if im in landscape or portrait , because i have a menu for landscape (that is the same for the desktop and tablet version) and then i have the portrait menu. The desktop and tablet version does not work well on portrait mode. !! This what i have to do :D, So right now im making the variables, true or false if im on that modes , to make the conditionals !! – Hugo Seleiro Sep 22 '17 at 07:49
  • @HugoSeleiro If that's the case you can refer here. https://stackoverflow.com/questions/4917664/detect-viewport-orientation-if-orientation-is-portrait-display-alert-message-ad – Thusitha Sep 22 '17 at 07:59