0

I want to detect if the device is desktop or mobile or tablet .. On this basis i want to change the image directory for eg

if (getDeviceState() == 'phone') {
              alert("phone");
          }
          else if (getDeviceState() == 'tablet') {
              alert("tablet");
          }
          else {
              alert("small-desktop");
          }

i have tried this using css and javascript

          var indicator = document.createElement('div');
          indicator.className = 'state-indicator';
          document.body.appendChild(indicator);

          // Create a method which returns device state
          function getDeviceState() {
              var index = parseInt(window.getComputedStyle(indicator).getPropertyValue('z-index'), 10);

              var states = {
                  2: 'small-desktop',
                  3: 'tablet',
                  4: 'phone'
              };

              return states[index] || 'desktop';
          }
          if (getDeviceState() == 'phone') {
              alert("phone");
          }
          else if (getDeviceState() == 'tablet') {
              alert("tablet");
          }
          else {
              alert("small-desktop");
          }

and in css

/* small desktop */
@media all and (max-width: 1200px) {
    .state-indicator {
        z-index: 2;
    }
}

/* tablet */
@media all and (max-width: 768px) {
    .state-indicator {
        z-index: 3;
    }
}

/* mobile phone */
@media all and (max-width: 360px) {
    .state-indicator {
        z-index: 4;
    }
}

But i am getting prob for landscape and potrait states..so its better to get useragent than by css

3
  • I'm not the one to give you the perfect answer to your question. But what I do know is there are a large number of very nice frameworks, jQuery plugins and other ways available to do the detection. Why bother writing your own? On your question: might be a idea to check how it's done in Twitter Bootstrap? Commented Jul 28, 2014 at 9:36
  • try this: jquerymobile.com/download Commented Jul 28, 2014 at 9:36
  • You can olso detect that using JS library like Modernizr Commented Jul 28, 2014 at 9:52

3 Answers 3

1

You can use device.js http://matthewhudson.me/projects/device.js/

source code and examples can be obtained from following link https://github.com/matthewhudson/device.js/tree/master

Use following methods to get the device type

device.mobile()
device.tablet() 

Use below to get the orientation:

device.landscape()
device.portrait()
Sign up to request clarification or add additional context in comments.

Comments

1

HTML has user agent field to send the device information:

PC

mozilla/5.0 (windows nt 6.3; wow64) applewebkit/537.36 (khtml, like gecko) chrome/36.0.1985.125 safari/537.36

iPhone

Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176 MicroMessenger/4.3.2

Windows Phone

UCWEB/2.0 (Windows; U; wds7.10; zh-CN; Nokia 900) U2/1.0.0 UCBrowser/8.6.0.199 U2/1.0.0 Mobile

  1. kernel engine

    IE、Gecko、WebKit、Opera

  2. browser type

    IE、Chrome、Firefox、Opera

  3. system

    Windows、Mac、Unix

  4. mobile device

    iPhone、iPod、Android、Nokia

You could detect like this:

    var ua = navigator.userAgent.toLowerCase();
    //check the device like this
    isChrome = ua.indexOf("chrome") > -1

Comments

0

Using only JS you can check window.outerHeight and window.outerWidth. So, combine different sizes you can define device type. Also, you can parse navigator.userAgent, which contains name of user browser.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.