I don’t know whether I should be here or not.
But I’m trying to make a website to detect on which device I am (iPhone/iPad)
and change the CSS-file I’m using.
I’m using this code:
function iCheck() {
if((navigator.userAgent.match(/iPad/i))) {
document.write("<link rel='stylesheet' type='text/css' href='../misc/iphone.css' />");
}
else if(navigator.userAgent.match(/iPhone/i)) {
document.write("<link rel='stylesheet' type='text/css' href='../misc/iphone.css' />");
}
}
but this gives me a blank page with nothing on it.
Can someone help me?
You can only call
document.writewhile the page is rendering (directly in<script>blocks).If you call it after the page finishes loading, it will replace the entire page.
To fix your issue, you can either run your code directly in the
<head>tag or replacedocument.writewith DOM manipulation (createElementandappendChild) to add a<link>tag to the<head>.