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