I currently use the following code to detect the screen size:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="FSAUX" style="height:1em;width:1em;left:100%;position:fixed;top:100%;"></div>
<div id="CMAUX" style="height:1cm;width:1cm;left:100%;position:fixed;top:100%;"></div>
<script type="text/javascript">
SCREEN = {
FBS: document.getElementById( 'FSAUX' ).offsetWidth,
CENTS: screen.width / document.getElementById( 'CMAUX' ).offsetWidth,
CHARS: screen.width / document.getElementById( 'FSAUX' ).offsetWidth,
WIDTH: screen.width
};
if( ( SCREEN.WIDTH < 800 ) ||
( SCREEN.CENTS < 10 ) ||
( SCREEN.CHARS < 80 ) ) {
document.title = 'Pantalla pequeña';
} else {
document.title = 'Pantalla grande';
}
</script>
</body>
</html>
I set minimum sizes for various factors: points, centimeters, and base font size. Depending on the above, I load one or another version of my application. Once one version is uploaded , I can't upload another one. They have very different interfaces and do not implement the same features.
The problem arises on steerable or rotatable devices . Depending on the orientation (horizontal/vertical) I get some or other screen values.
This means that it loads the version for small screens, when in reality the user would just have to rotate the device, placing it horizontally to be able to use the version for large screens .
I don't want to detect the browser. I am only interested in knowing if the device is orientable or not .
Is there a way to check it in Javascript? Preferably pure ; If a library is needed, I'll search its code to see how it implements it.
Starting you already have a problem. Having a mobile version and another for desktop is an unnecessary waste of time. With the emergence of the responsive design trend, this should go down in history.
If possible. Not only is it possible to know if it is adjustable, but also the orientation of the device (portrait or landscape). The property
window#orientation
usually returns the values:These last two values can change depending on the device, so you must be careful.
For example, the Apple Touch interprets the last value as -90, while the Google Nexus interprets it as 90, which will not let you know if the device is upside down . You may not see it necessary in general, but it's better if you ever need it, it shows you a standard value for all. Another example of this is the Toshiba tablets: most series will show you 180 if you turn it upside down, when the logical thing is for it to be -90, as Google or Amazon does.
If you need to know the orientation of the device, it can be useful
window#screen.orientation
whose propertytype
allows you to know the orientation of the device, which are:The primary/secondary is used to interpret whether it is 'upside down' or not.
Note: The property
window#orientation
will only be available on devices that support orientation.There is a web API in charge of screen orientation ( Screen Orientation API ) that allows you to read the type and angle of orientation, know if there are changes in it, and even "lock" orientation changes.
The main problem is that it is experimental and not widely supported. On desktop devices the latest versions of almost all browsers allow this (except Safari), although you may need to use prefixes (see below). And on mobile devices, Chrome and Firefox Mobile yes... but little else.
With this API you can:
Detect device orientation :
As this is an experimental technology, you may need to use device prefixes, something like this (described in the link in this section):
The variable
orientacion
will then have one of the following values (an example of how it could be used to ask the user to change the orientation if it's not one you want is on the linked page):Lock device orientation with
lockOrientation
:This would allow you to lock the screen in any of the modes you want (those listed above). It can be a single value or an array. Here is an example to force the screen orientation to stay in landscape mode:
Detect orientation changes : You could still do this with
onorientationchange
, but this API adds one more possibility by allowing you to associate an event handlerchange
with the screen orientation (screen.orientation
).Something like this:
I understand that you can't exactly detect if the device is steerable (which is what you're asking), but you can detect the orientation and then display a message telling the user "You'd better rotate the device" or force them to rotate it by turning all the way around. content for what you want.