The JavaScript screen.witdth property does not work in GAS environment. The Google Caja sanitizer, which is under the hood of GAS, throws the
Uncaught script error: 'screen is not defined.' in source:
'tryit.asp?filename=tryjsref%5fscreen%5fwidth' at line: 7
tryit.asp?filename=tryjsref%5fscreen%5fwidth:7: screen is not defined.
exception if to load the following HTML page to the Caja Playground. Probably it is an issue of Caja.
<!DOCTYPE html>
<html>
<body>
<script>
document.write("Test");
document.write("Total Width: " + screen.width);
</script>
</body>
</html>
A possible workaround is not to rely upon a screen resolution but to use a user-agent string. This string contains the user browser "id". The UiApp.getUserAgent method returns it. For instance, Mozilla Firefox on a desktop provides this string like - Mozilla/5.0 (Windows NT 6.0; rv:15.0) Gecko/20100101 Firefox/15.0.1,gzip(gfe)
. On a mobile phone this string contains ...(Android; Mobile; rv:15.0)...
. A check procedure searches in the returned string the words Mobile
, Android
, etc. If one of them found it is a mobile platform and the main code draws a mobile GUI using relative controls sizes. The following code shows how to use it
function doGet(e) {
var app = UiApp.createApplication();
app.add(app.createLabel(UiApp.getUserAgent()));
return app;
}