简体   繁体   English

在现代浏览器中加载特定于浏览器的样式表

[英]Loading Browser Specific Style Sheets in Modern Browsers

I really need some help here. 我真的需要一些帮助。 I have searched the forum and the internet for hours. 我已经在论坛和互联网上搜索了几个小时。 I have found many articles but they either have no definitive working answers on how to do this or they are too old. 我发现了很多文章,但是他们要么对此没有确定的工作答案,要么它们太老了。

I have 2 stylesheets. 我有2个样式表。 One for IE and one for all other browsers. 一种用于IE,另一种用于所有其他浏览器。 This is not ideal I know but this was a complicated project and it is the way things developed. 我知道这并不理想,但这是一个复杂的项目,这是事物发展的方式。 One sheet is 3D (webkit) and the other is 2D (IE). 一张纸是3D(webkit),另一张纸是2D(IE)。 I need to be able to load the CSS based on the browser. 我需要能够基于浏览器加载CSS。 I only need the 2 options, IE an other. 我只需要2个选项,另一个即可。

I have tried conditional statements and I have tried browser detection. 我尝试了条件语句,并且尝试了浏览器检测。 Must work for modern browsers. 必须适用于现代浏览器。

I have tried the following in the head and nothing has worked. 我已经尝试过以下方法,但没有任何效果。 The webkit CSS either overrides the IE CSS or it doesn't load at all. 该webkit CSS要么覆盖IE CSS,要么根本不加载。 Please assist. 请协助。

Attempt 1 (problem - webkit css overrides IE CSS even though it is commented): 尝试1(问题-即使有注释,webkit css也会覆盖IE CSS):

<!--[if !IE]><!-->   

<link rel="stylesheet" type="text/css" href="http://secure.cart32.com/WarrenKahn/SolarWebkit.css" />

<!--<![endif]-->

<!--[if gte IE 9]><!-->   

  <link rel="stylesheet" type="text/css" href="http://secure.cart32.com/WarrenKahn/SolarIE.css" />

<!--<![endif]-->

<!--[if lte IE 9]><!-->   

  <link rel="stylesheet" type="text/css" href="http://secure.cart32.com/WarrenKahn/SolarIE.css" />

<!--<![endif]-->

Attempt 2 (problem - won't load at all in any browser): 尝试2(问题-完全不会在任何浏览器中加载):

var nAgt = navigator.userAgent;
var browserName  = navigator.appName;
var nameOffset,verOffset,ix;

// In Opera, the true version is after "Opera" or after "Version"
if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
 browserName = "Opera";
}
// In MSIE, the true version is after "MSIE" in userAgent
else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
 browserName = "Microsoft Internet Explorer";
}
// In Chrome, the true version is after "Chrome" 
else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
 browserName = "Chrome";
}
// In Safari, the true version is after "Safari" or after "Version" 
else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
 browserName = "Safari";
}
// In Firefox, the true version is after "Firefox" 
else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
 browserName = "Firefox";
}
// In most other browsers, "name/version" is at the end of userAgent 
else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < 
          (verOffset=nAgt.lastIndexOf('/')) ) 
{
 browserName = nAgt.substring(nameOffset,verOffset);
 fullVersion = nAgt.substring(verOffset+1);
 if (browserName.toLowerCase()==browserName.toUpperCase()) {
  browserName = navigator.appName;
 }
}

 if (browserName == "Microsoft Internet Explorer") {
 document.write("<link type=\"text/css\" rel=\"stylesheet\" href=\"http://secure.cart32.com/WarrenKahn/SolarIE.css">");
 }
 else {
 document.write("<link type=\"text/css\" rel=\"stylesheet\" href=\"http://secure.cart32.com/WarrenKahn/SolarWebkit.css">");
 }

I made other attempts but these seemed like the most valid. 我做了其他尝试,但这些尝试似乎是最有效的。 Any working solutions are welcome. 任何可行的解决方案都欢迎。 Thanks in advance. 提前致谢。

Can you render the 2D page by default, but redirect to the 3D page if you detect (via feature detection) that the incoming browser supports what you are trying to do? 您可以默认渲染2D页面,但是如果您检测到(通过功能检测)传入的浏览器支持您尝试执行的操作,则可以重定向到3D页面?

eg 例如

<script>
  if(typeof(navigator.fancyNewThing) == 'function'){
    location.href = '3Dpage.html';
  }
</script>

or load your CSS accordingly. 或相应地加载CSS。

Your browser detection code (above in the question) is a bit of overkill. 您的浏览器检测代码(问题上方)有点过分。 Though singling out a browser by its user agent isn't always the wisest thing to do, this script should identify IE for you. 尽管通过用户代理选择浏览器并不总是最明智的选择,但是此脚本应为您标识IE。

if(navigator.userAgent.indexOf('MSIE') != -1){
  document.write("<link type=\"text/css\" rel=\"stylesheet\" href=\"http://secure.cart32.com/WarrenKahn/SolarIE.css\">");
} else {
  document.write("<link type=\"text/css\" rel=\"stylesheet\" href=\"http://secure.cart32.com/WarrenKahn/SolarWebkit.css\">");
}

Some suggestions: 一些建议:

Server-side detection 服务器端检测

Depending on the server serving the CSS files, you may configure/customize it to serve the file you want depending on the User-Agent header. 根据提供CSS文件的服务器,您可以根据User-Agent标头配置/自定义它以提供所需的文件。

Link to CSS depending on the browser 链接到CSS,具体取决于浏览器

See answer from scunliffe 查看 scunliffe的回答

Single CSS with class on body and overridden rules 具有正文和覆盖规则的类的单个CSS

Detect the browser, and add a class on the body according to the browser, for example document.body.className += " msie" 检测浏览器,并根据浏览器在正文上添加一个类,例如document.body.className += " msie"

Then override the rules you want in your css 然后覆盖您想要的CSS规则

body.msie someTag { /*msie specific rules*/ }

I recommend to use a library for browser detection, they are usually more tested. 我建议使用库进行浏览器检测,它们通常经过更多测试。 I just found WhichBrowser which seems to do that very well. 我刚刚发现WhothBrowser看起来做得很好。

With some great suggestions from the users here I was able to get this right! 在这里的用户提出了一些很棒的建议后,我才能够正确地做到这一点! IE11 was the culprit here. IE11是这里的罪魁祸首。 MS prematurely defined IE11 as a webkit browser so certain properties like the user agent have been updated. MS过早将IE11定义为Webkit浏览器,因此某些属性(如用户代理)已更新。 Here's an article on that. 这是一篇关于此的文章。 http://www.nczonline.net/blog/2013/07/02/internet-explorer-11-dont-call-me-ie/ http://www.nczonline.net/blog/2013/07/02/internet-explorer-11-dont-call-me-ie/

So MSIE or RV weren't working because it confused the webkit browsers. 因此MSIE或RV无法正常工作,因为它混淆了Webkit浏览器。 I used Trident instead. 我改用三叉戟。

The following script works for dynamically loading CSS in the most recent versions of all browsers on the client side: 以下脚本用于在客户端上所有浏览器的最新版本中动态加载CSS:

if(navigator.userAgent.indexOf('Trident') != -1){
  document.write("<link type=\"text/css\" rel=\"stylesheet\" href=\"http://secure.cart32.com/WarrenKahn/SolarIE.css\">");
  } 


  else {
  document.write("<link type=\"text/css\" rel=\"stylesheet\" href=\"http://secure.cart32.com/WarrenKahn/SolarWebkit.css\">");

}

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM