开发者

Detect Chrome browser in HTML

How can I detect the Google Chrome browser? For example, I can detect Internet Explorer with this tag:

<!--[if lte IE]>
<link href="ie_only.css" rel="stylesheet" type="text/css" />
<![endif]-->

I have one idea:

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

But maybe somebody has a better solution?

Update: My page shows wrong only with Chrome browser, so i need load (only with chrome开发者_JAVA技巧) special css file with updates


As mentioned in the comments above, you should be detecting feature compatibility, not browsers. That said, there are rare occasions when you need to target WebKit browsers in your CSS. For example, I had a minor font size/rendering issue that only appeared in Chrome/Safari, and I fixed it with something like this:

@media screen and (-webkit-min-device-pixel-ratio:0) {
  /* Chrome- and Safari-specific CSS here*/
}

Another warning: If something renders incorrectly in Chrome/Safari but not in Firefox, there's a good chance you have some other underlying issue, e.g. invalid markup. Be sure to validate your HTML and check for other problems before using the (slightly hackish) method above.


var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if (is_chrome) {
    document.write("Yaii it is Chrome only")
}

From: "Simple way to detect chrome browser using javascript"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜