PHP/HTML/CSS - 如果是 FireFox,如果是 Chrome,如果是 Safari

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12089942/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 19:57:30  来源:igfitidea点击:

PHP/HTML/CSS - If FireFox, If Chrome, If Safari

cssfirefoxgoogle-chromeif-statementconditional

提问by TheBlackBenzKid

Is there a simple conditional statement, css command, html, jquery, javascript or simple PHP dynamic way of detecting the current browser?

是否有简单的条件语句、css 命令、html、jquery、javascript 或简单的 PHP 动态检测当前浏览器的方式?

<!if firefox>
    .element { top:4px; }
<![endif]>
<!if chrome>
    .element { top:6px; }
<![endif]>
<!if ie>
    .element { top:8px; }
<![endif]>
<!if opera>
    .element { top:10px; }
<![endif]>
<!if safari_webkit>
    .element { top:12px; }
<![endif]>

Can this Psuedo code be done in jQuery/JS/HTML or CSS PHP etc?

这个伪代码可以在 jQuery/JS/HTML 或 CSS PHP 等中完成吗?

回答by AlphaApp

With CSS there is no way you can achieve browser detection. However with PHP, ASP and other programming languages you can get browser detection within the page. I am not here to tell you the pro or cons about it - I take it you know about the bad and good about browser detection and web standards but here is the list.

使用 CSS 无法实现浏览器检测。但是,使用 PHP、ASP 和其他编程语言,您可以在页面内进行浏览器检测。我不是来告诉你关于它的利弊 - 我认为你知道浏览器检测和网络标准的优缺点,但这里是列表。

PHP solution.

PHP 解决方案。

if(isset($_SERVER['HTTP_USER_AGENT'])){
    $agent = $_SERVER['HTTP_USER_AGENT'];
}

Then, compare it to what you want

然后,将它与您想要的进行比较

For compare with, for example "firefox" you should do:

为了与例如“firefox”进行比较,您应该执行以下操作:

if(strlen(strstr($agent,"Firefox")) > 0 ){      
    $browser = 'firefox';
}
if($browser=='firefox'){
    echo '<style type="text/css">.element{top:2px}';
}

jQuery solution.

jQuery 解决方案。

// Safari CSS and Webkit Google Chrome
if ($.browser.webkit) {
   $("#element").css('top', '2px');
} else if ( $.browser.safari ) //not fully supported on 1.7 jQuery {
   $("#element").css('top', '2px');
// Opera CSS
} else if ( $.browser.opera ) {
   $("#element").css('top', '2px');
// Internet Explorer CSS
} else if ( $.browser.msie ) {
   $("#element").css('top', '2px');
// Mozilla FireFox CSS
} else if ( $.browser.mozilla ) {
   $("#element").css('top', '2px');
// Normal Revert, careful and note your the use of !important
} else {
   $("#element").css('top', '2px');
   // You can have normal JavaScript between these too
   document.getElementById("element").style.top="2px";
}

Mootools solution.

Mootools 解决方案。

if (Browser.ie){
    // This code will only run in IE
}

if (Browser.firefox2){
    // This code will only run in Firefox 2
}
if (Browser.firefox){
    // This code will only run in Firefox 
} 
if (Browser.chrome){
    // This code will only run in Chrome
} 
if (Browser.opera){
    // This code will only run in Chrome
}   
if (Browser.ie6 || Browser.ie7){
    // Please upgrade your browser
}
// Also notice you can use Engine.trident
if(Browser.Engine.trident) {

}

Prototype solution.

原型解决方案。

if(Prototype.Browser.IE){
  // do something IE specific
}
if(Prototype.Browser.Opera){
  // do something Opera specific
}
if(Prototype.Browser.WebKit){
  // do something WebKit specific
}
if(Prototype.Browser.MobileSafari){
  // do something MobileSafari specific - iPhone etc
}
if(Prototype.Browser.Gecko){
  // do something Gecko specific
}

回答by Matthew Felgate

To do this with CSS only.

仅使用 CSS 执行此操作。

You can target Firefox with this 'hack':

您可以使用此“hack”来定位 Firefox:

@-moz-document url-prefix() {...}

And Chrome & Safari together like this:

和 Chrome 和 Safari 一起像这样:

@media screen and (-webkit-min-device-pixel-ratio:0) {...}

But not necessarily recommended...

但不一定推荐...

回答by Evandro Silva

Using javascript:

使用javascript:

navigator.appCodeName

Stores the browser codename:

存储浏览器代号:

navigator.appName

Is the name of the browser.

是浏览器的名称。

But I would recommend using jQuery for more efficiency and less headaches:

但我建议使用 jQuery 来提高效率并减少麻烦:

if ($.browser.webkit) {
   $("#div ul li").css( "display","inline-table" );
} else if ( $.browser.msie ) {
   $("#div ul li").css( "display","inline" );
} else {
   $("#div ul li").css( "display","inline-table" );
}

EDIT:According to jQuery.com:

编辑:根据 jQuery.com:

  • webkit (Chrome and Safari)

  • safari (deprecated)

  • opera

  • msie (Internet Explorer)

  • mozilla (Firefox)

  • webkit(Chrome 和 Safari)

  • 野生动物园(已弃用)

  • 歌剧

  • msie (Internet Explorer)

  • 火狐浏览器)

Source: JQuery Site

资料来源:JQuery 网站

回答by Sohail Yasmin

In php you can use this code to detect browsers

在 php 中,您可以使用此代码来检测浏览器

<?php
  $msie = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE') ? true : false;
  $firefox = strpos($_SERVER["HTTP_USER_AGENT"], 'Firefox') ? true : false;
  $safari = strpos($_SERVER["HTTP_USER_AGENT"], 'Safari') ? true : false;
  $chrome = strpos($_SERVER["HTTP_USER_AGENT"], 'Chrome') ? true : false;
?>