开发者

Detect iPhone/iPad and change CSS file

开发者 https://www.devze.com 2023-02-03 10:40 出处:网络
I don\'t know whether I should be here or not. B开发者_Python百科ut I\'m trying to make a website to detect on which device I am (iPhone/iPad)

I don't know whether I should be here or not.

B开发者_Python百科ut 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>.

0

精彩评论

暂无评论...
验证码 换一张
取 消