当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Detect iPhone/iPad and change CSS file

问题描述:

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.write while 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 replace document.write with DOM manipulation (createElement and appendChild) to add a <link> tag to the <head>.

分享给朋友:
您可能感兴趣的文章:
随机阅读: