Switching stylesheet with Javascript based on browser

爱⌒轻易说出口 提交于 2019-12-04 10:44:01

Can´t you use media queries to determine what stylesheet the browser must use? Checking for individual user agents seems the wrong way to go about it.

Edit: I just noticed the media queries code you are using and I noticed you are using a different path: css/style_mob.css when using the javascript and ../css/style_mob.css. Is that a typo or could it be the reason it's not working?

Phone browsers cache pages just like desktop browsers do. Before you spend too much time looking into the problem, make sure it's not a caching issue.

Clear the cache on your phone and visit the URL again. Since the CSS loads correctly after a refresh, I wouldn't be surprised if this solved everything.

Also, you mention in your question that it's better to have a separate domain altogether. I say not necessarily. There are many reasons why you would only want to have 1 domain for every platform. @media queries and responsive web design can go great ways. If you are interested in the subject, you should definitely check out this article from A list apart. It could give you an idea of what the possibilities are.

EDIT:

If it still doesn't work (which it doesn't seem to) then perhaps you should test your user agent code. Instead of loading your style.css style for everyone, remove that statement, and make it conditional to a browser not being android :

Remove the css line from your html :

<link href="css/style.css" id="stylesheet" rel="stylesheet" type="text/css" />

And then in your javascript :

var normalCss = document.createElement('<link href="css/style.css" id="stylesheet" rel="stylesheet" type="text/css" />');
var mobileCss = document.createElement('<link href="css/mobile.css" id="stylesheet" rel="stylesheet" type="text/css" />');

if(userAgent.search("Android")>=0){
    document.getElementsByTagName("head")[0].appendChild(mobileCss);
}
else {
    document.getElementsByTagName("head")[0].appendChild(normalCss);
}

Clear your cache and test it. Does it load a css the first time or does it load a page without styles?

If it doesn't load the first time, this might mean that the javascript code isn't run on time. How are you calling it? Is it in the head section? Are you using jQuery with $(document).ready or something like that?

I have found a workaround for the problem of the android browser not loading a mobile css stylesheet unless you refresh the page. I call this a work around and not a permanent solution because I have one website where android loads the mobile css on the first try without a problem using:

<link media="handheld, only screen and (max-device-width: 480px)" href="mobile.css" type="text/css" rel="stylesheet" />

But I have another website that uses the same code above to load the css and the android browser refuses to use it unless I refresh the page. I have tried changing many parts of the html to match the website that does work but all to no avail. So here is the workaround code:

<link media="handheld, only screen and (max-width: 480px), only screen and (max-device-width: 480px)" href="mobile.css" type="text/css" rel="stylesheet" />

“handheld” allows older mobile phones to load mobile.css

“only screen and (max-width: 480px)” forces android devices to load mobile.css on the first try if they have a width of 480px or less.

“only screen and (max-device-width: 480px)” allows new blackberry phones, iphones and USUALLY android devices to load mobile.css if they have a width of 480px or less

Windows mobile 7 phones with IE do not use the new media tags so you have to use this code to get them to load mobile.css:

<!--[if IEMobile 7]>
<link rel="stylesheet" type="text/css" href="mobile.css" media="screen" />
<![endif]-->
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!