且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Javascript来检测Google Chrome以切换CSS

更新时间:2022-04-28 21:53:38

var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

资料来源: http://davidwalsh.name/detecting-google-chrome-javascript

更新(2015-07-20)

上述解决方案并不总是奏效。有关更可靠的解决方案,请参见此答案(见下文)。话虽如此,我会避免浏览器检测,并改用功能检测: / p>

The above solution does not always work. A more reliable solution can be found in this answer (see below). That being said, I would avoid browser detection and go with feature detection instead:

var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor); 

您可以包含专门用于chrome的css文件,例如:

You can include a css file specifically for chrome like this:

if (isChrome) {
    document.write('<'+'link rel="stylesheet" href="../component/chromeDefault.css" />');
}

UPDATE(2014-07-29):

@gillesc有一个更优雅的建议检测Chrome,他在下面的评论中发布了,它也可以在这个问题

@gillesc has a more elegant suggestion for detecting Chrome which he posted in a comment below and it can also be viewed on this question.

var isChrome = !!window.chrome