基于浏览器使用Javascript切换样式表

发布于 2024-10-31 03:58:55 字数 1613 浏览 4 评论 0原文

我目前正在尝试为使用 Adob​​e Contribute 的客户端编辑一个网站,因此如果用户通过手机浏览,它会使用不同的样式表。手机样式表要简单得多,并删除背景图像和样式以减少加载时间。我知道最好的做法是为移动设备建立一个完全独立的网站,但这对于客户来说并不实用,因为他需要能够使用 Contribute 更新其网站内容。

这已经在一些设备上工作了,比如 iPhone(我上次测试过!),但在我的 Android 手机上我遇到了一个奇怪的问题。当我访问 URL 或单击链接时,它会使用默认样式表显示网站。如果我刷新页面,它就会决定显示使用移动样式表呈现的网站。在我单击刷新之前,几乎就像我的代码没有选取 UserAgent 一样。

这是我正在使用的代码:

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

<script type="text/javascript">
    var userAgent = navigator.userAgent;

    if(userAgent.search("Mobile")>=0) document.getElementById("stylesheet").href = "css/style_mob.css";

    if(userAgent.search("Nokia")>=0) document.getElementById("stylesheet").href = "css/style_mob.css";

    if(userAgent.search("SymbianOS")>=0) document.getElementById("stylesheet").href = "css/style_mob.css";
</script>

该脚本仅针对少数设备显示,但您可以了解总体思路。

我的用户代理字符串如下:

Mozilla/5.0(Linux;U;Android 2.1-更新1;英-GB; T-Mobile_G2_Touch 版本/ERE27) AppleWebKit/530.17 (KHTML,如 Gecko)版本/4.0 移动版 Safari/530.17

编辑:

我应该补充一点,我曾尝试使用媒体查询,但这在 iPhone 或 Android 上根本不起作用。上面的代码在 iPhone 上运行得很好。下面是我尝试将其与媒体查询一起使用时使用的代码。

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<link href="../css/style.css" id="stylesheet" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" media="only screen and (max-width: 480px), only screen and (max-device-width: 480px)" href="../css/style_mob.css" />

I'm currently trying to edit a website for a client which uses Adobe Contribute so it uses a different stylesheet if the user is browsing from a mobile phone. The mobile phone stylesheet is much simpler and strips out background images and styles to reduce load time. I understand it would be better practice to have a completely seperate website for the mobile, but this is not practical for the client as he needs to be able to update his websites content using Contribute.

This is working in some devices already such as the iPhone (last time I tested!) but on my Android phone I have a strange problem. When I visit the URL or click on a link, it displays the website using the default stylesheet. If I refresh the page it then decides to show the site rendered with the mobile stylesheet. It is almost as though the UserAgent is not being picked up by my code until I click refresh.

Here is the code I am using:

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

<script type="text/javascript">
    var userAgent = navigator.userAgent;

    if(userAgent.search("Mobile")>=0) document.getElementById("stylesheet").href = "css/style_mob.css";

    if(userAgent.search("Nokia")>=0) document.getElementById("stylesheet").href = "css/style_mob.css";

    if(userAgent.search("SymbianOS")>=0) document.getElementById("stylesheet").href = "css/style_mob.css";
</script>

The script only shows for a few devices but you get the general idea.

My User Agent string is as follows:

Mozilla/5.0 (Linux; U; Android
2.1-update1; en-gb; T-Mobile_G2_Touch Build/ERE27) AppleWebKit/530.17
(KHTML, like Gecko) Version/4.0 Mobile
Safari/530.17

EDIT:

I should add that I have tried to use media queries but this does not work at all on either the iPhone or Android. The code above works a treat on the iPhone. Below is the code I am using when trying to have this work with media queries.

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<link href="../css/style.css" id="stylesheet" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" media="only screen and (max-width: 480px), only screen and (max-device-width: 480px)" href="../css/style_mob.css" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不奢求什么 2024-11-07 03:58:55

你不能使用 媒体查询 来确定浏览器必须使用哪种样式表?检查单个用户代理似乎是错误的方法。

编辑:我刚刚注意到您正在使用的媒体查询代码,并且我注意到您在使用 javascript 和 时使用了不同的路径:css/style_mob.css。 ./css/style_mob.css。这是一个错字还是可能是它不起作用的原因?

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?

谁人与我共长歌 2024-11-07 03:58:55

手机浏览器像桌面浏览器一样缓存页面。在花费太多时间研究问题之前,请确保这不是缓存问题。

清除手机缓存并再次访问该网址。由于刷新后 CSS 加载正确,因此如果这解决了所有问题,我不会感到惊讶。

另外,您在问题中提到最好完全拥有一个单独的域。我说不一定。您只想为每个平台拥有 1 个域名的原因有很多。 @media 查询和响应式网页设计可以发挥很大作用。
如果您对这个主题感兴趣,您一定要查看这篇文章分开列出一个清单。它可以让您了解可能性是什么。

编辑:

如果它仍然不起作用(看起来不起作用)那么也许您应该测试您的用户代理代码。
不要为每个人加载 style.css 样式,而是删除该语句,并使其以浏览器不是 android 为条件:

从 html 中删除 css 行:

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

然后在 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);
}

清除缓存并测试它。它是第一次加载 css 还是加载没有样式的页面?

如果第一次没有加载,这可能意味着 javascript 代码没有按时运行。你怎么称呼它?是在头部吗?您是否将 jQuery 与 $(document).ready 或类似的东西一起使用?

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?

热血少△年 2024-11-07 03:58:55

我找到了解决 Android 浏览器除非刷新页面否则无法加载移动 CSS 样式表问题的解决方法。我称这是一个解决方案,而不是一个永久的解决方案,因为我有一个网站,android在第一次尝试时加载移动css,没有问题,使用:

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

但我有另一个网站,使用上面相同的代码来加载css和android浏览器除非我刷新页面,否则拒绝使用它。我尝试更改 html 的许多部分以匹配有效的网站,但都无济于事。因此,这里是解决方法代码:

<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”允许较旧的手机加载 mobile.css

“only screen and (max-width: 480px)”强制 Android 设备在宽度为 480px 时在第一次尝试时加载 mobile.css或更少。

“only screen and (max-device-width: 480px)” 允许新的黑莓手机、iPhone 和通常的 Android 设备加载 mobile.css(如果它们的宽度为 480px 或更小)

带 IE 的 Windows mobile 7 手机不使用新媒体标签,因此您必须使用此代码来让它们加载 mobile.css:

<!--[if IEMobile 7]>
<link rel="stylesheet" type="text/css" href="mobile.css" media="screen" />
<![endif]-->

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]-->
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文