Javascript 使用Javascript检测谷歌浏览器切换CSS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10251149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Javascript to detect Google Chrome to switch CSS
提问by Jakob
I have been playing around with different scripts, I found one that works for everything but Chrome... this is the code I have been using to differ in .CSS files. I tried just makeing the Browser name into "Chrome" But that did not work.
我一直在玩不同的脚本,我发现一个适用于除 Chrome 之外的所有脚本......这是我一直用来在 .CSS 文件中不同的代码。我尝试将浏览器名称设为“Chrome”,但这不起作用。
if (browser == 'Firefox') {
document.write('<link rel="stylesheet" href="../component/fireFoxdefault.css" />');
}
if (browser == 'Safari') {
document.write('<'+'link rel="stylesheet" href="../component/default.css" />');
}
回答by TJ.
Use the following to detect chrome:
使用以下方法检测铬:
var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
Source: http://davidwalsh.name/detecting-google-chrome-javascript
来源:http: //davidwalsh.name/detecting-google-chrome-javascript
Update (2015-07-20):
更新(2015-07-20):
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);
You can include a css file specifically for chrome like this:
您可以像这样包含一个专门用于 chrome 的 css 文件:
if (isChrome) {
document.write('<'+'link rel="stylesheet" href="../component/chromeDefault.css" />');
}
UPDATE (2014-07-29):
更新(2014-07-29):
@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.
@gillesc 有一个更优雅的建议来检测他在下面的评论中发布的 Chrome,也可以在这个问题上查看。
var isChrome = !!window.chrome
回答by Rrjrjtlokrthjji
Here are two simple methods,one using indexOf and one using test :
这里有两种简单的方法,一种使用 indexOf ,一种使用 test :
// first method
function check_chrome_ua() {
var ua = navigator.userAgent.toLowerCase();
var is_chrome = /chrome/.test(ua);
return is_chrome;
}
// second method */
function check_chrome_ua2() {
var ua = navigator.userAgent.toLowerCase();
var is_chrome = ua.indexOf("applewebkit/") != -1 && ua.indexOf("khtml") > - 1;
return is_chrome;
}
alert(check_chrome_ua()); // false or true
alert(check_chrome_ua2()); // false or true
After checking if chrome is in use or not with one of these two boolean functions,you can implement your method on changing the stylesheet.
在使用这两个布尔函数之一检查 chrome 是否正在使用后,您可以实现更改样式表的方法。
回答by Arnaud Leyder
Update for Chrome on iOS: Chrome 38 (tested on iOS 8.1) returns false on !!window.chrome
. To fix this you can use:
iOS 上的Chrome更新:Chrome 38(在 iOS 8.1 上测试)在 上返回 false !!window.chrome
。要解决此问题,您可以使用:
function isChrome(){
var windowChrome = !!window.chrome;
if(!windowChrome){
// Chrome iOS specific test
var pattern = /crios/i;
return pattern.test(window.navigator.userAgent);
}else{
return windowChrome;
}
}
Google referenceon the matter
关于此事的谷歌参考
回答by Satinder singh
var userAgent = navigator.userAgent.toLowerCase();
$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
// Is this a version of Chrome?
if($.browser.chrome){
userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
userAgent = userAgent.substring(0,userAgent.indexOf('.'));
$.browser.version = userAgent;
// If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
$.browser.safari = false;
}
// Is this a version of Safari?
if($.browser.safari){
userAgent = userAgent.substring(userAgent.indexOf('safari/') +7);
userAgent = userAgent.substring(0,userAgent.indexOf('.'));
$.browser.version = userAgent;
}