Seeking Clarity:
I have a straightforward project involving a Google Script / Web App, but I am struggling to find a way to load a specific CSS file based on the user's device.
In other words, rather than focusing on responsive design, I need to load different CSS files depending on the user's device for certain requirements.
My setup consists of:
Index.html | css1.html | css2.html
Here is the code snippet from my Index.html file:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<? if (/Mobi/.test(navigator.userAgent)) {?>
<?!= HtmlService.createHtmlOutputFromFile('css1').getContent(); ?>
<?} else {?>
<?!= HtmlService.createHtmlOutputFromFile('css2').getContent(); ?>
<?}?>
</head>
<body>
<p> THIS is a TEST </p>
</body>
</html>
However, when I deploy and test this code, I encounter an error: ReferenceError: navigator is not defined (line 5)
The code referenced above that works can be found here.
I have tried alternative methods like @media load, but none seem to work. While I can load different pages based on the user's device, I would prefer it to be a CSS-specific solution.
---> Edit: code.gs
function doGet(e)
{
var html = HtmlService.createTemplateFromFile('Index');
var pageData = html.evaluate()
.setTitle('Web App 1')
.setFaviconUrl('https://www.gstatic.com/images/icons/material/product/2x/apps_script_64dp.png')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.addMetaTag('viewport', 'width=device-width, initial-scale=1')
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
return pageData;
}
function include(filename)
{
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
};
---> css1.html
<style>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css"
integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB"
crossorigin="anonymous">
<style>
p {
color: blue;
}
</style>
---> css2.html
<style>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css"
integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB"
crossorigin="anonymous">
<style>
p {
color: red;
}
</style>
In attempting @Media approach in the <head>
, I used the following which did not yield results:
<link rel="stylesheet" media='screen and (min-width: 140px) and (max-width: 380px)' href="css1"/>
<link rel="stylesheet" media='screen and (min-width: 381px) and (max-width: 700px)' href="css2"/>