doupi3874 2013-03-04 07:12
浏览 42
已采纳

优化条件语句(PHP和JavaScript)

I have been using two types of conditional statements of my own creation on my website. I use these statements to detect browsers and to output different stylesheet. (I know there are better and more complex ways of doing this, but this is beside the point.) As you can see, MSIE 8, 9, 10, etc. use the same stylesheet, but I am not quite sure as to how to group the statements:

<?php
if (strStr(getEnv('HTTP_USER_AGENT'), 'Opera')) 
{ 
  echo '<link rel="stylesheet" href="http://www.example.com/nn_styles.css" type="text/css" media="screen">' . "
" . '<style type="text/css">@import url("http://www.example.com/op11_styles.css");</style>' . "
"; 
} 
else if (strStr(getEnv('HTTP_USER_AGENT'), 'Gecko')) 
{ 
  echo '<link rel="stylesheet" href="http://www.example.com/nn_styles.css" type="text/css" media="screen">' . "
" . '<style type="text/css">@import url("http://www.example.com/ch_styles.css");</style>' . "
"; 
}
else if (strStr(getEnv('HTTP_USER_AGENT'), 'MSIE 6.0')) 
{ 
  echo '<link rel="stylesheet" href="http://www.example.com/ie_styles.css" type="text/css" media="screen">' . "
" . '<style type="text/css">@import url("http://www.example.com/ie6_styles.css");</style>' . "
"; 
}
else if (strStr(getEnv('HTTP_USER_AGENT'), 'MSIE 7.0')) 
{ 
  echo '<link rel="stylesheet" href="http://www.example.com/ie7_styles.css" type="text/css" media="screen">' . "
" . '<style type="text/css">@import url("http://www.example.com/ie7_9_styles.css");</style>' . "
"; 
}
else if (strStr(getEnv('HTTP_USER_AGENT'), 'MSIE 8.0')) 
{ 
  echo '<style type="text/css">@import url("http://www.example.com/ie7_styles.css");</style>' . "
" . '<style type="text/css">@import url("http://www.example.com/ie7_9_styles.css");</style>' . "
"; 
}
else if (strStr(getEnv('HTTP_USER_AGENT'), 'MSIE 9.0')) 
{ 
  echo '<style type="text/css">@import url("http://www.example.com/ie7_styles.css");</style>' . "
" . '<style type="text/css">@import url("http://www.example.com/ie7_9_styles.css");</style>' . "
"; 
}
else if (strStr(getEnv('HTTP_USER_AGENT'), 'MSIE 10.0')) 
{ 
  echo '<style type="text/css">@import url("http://www.example.com/ie7_styles.css");</style>' . "
" . '<style type="text/css">@import url("http://www.example.com/ie7_9_styles.css");</style>' . "
"; 
}
?>

This is the JS version of the same script:

var MSIE10=navigator.userAgent.indexOf("MSIE 10.0");
var MSIE9=navigator.userAgent.indexOf("MSIE 9.0");
var MSIE8=navigator.userAgent.indexOf("MSIE 8.0");
var MSIE7=navigator.userAgent.indexOf("MSIE 7.0");
var MSIE6=navigator.userAgent.indexOf("MSIE 6.0");
var NETS=navigator.userAgent.indexOf("Gecko");
var OPER=navigator.userAgent.indexOf("Opera");
if(OPER>-1) {
document.write('<link rel="stylesheet" href="http://www.example.com/op_styles.css" type="text/css">');
document.write('<style type="text/css">@import url("http://www.example.com/op11_styles.css");</style>');
}
else if(MSIE6>-1){
document.write('<link rel="stylesheet" href="http://www.example.com/ie_styles.css" type="text/css">');
document.write('<style type="text/css">@import url("http://www.example.com/ie6_styles.css");</style>');
}
else if(MSIE7>-1){
document.write('<link rel="stylesheet" href="http://www.example.com/ie7_styles.css" type="text/css">');
document.write('<style type="text/css">@import url("http://www.example.com/ie7_9_styles.css");</style>');
}
else if(MSIE8>-1){
document.write('<style type="text/css">@import url("http://www.example.com/ie7_styles.css");</style>');
document.write('<style type="text/css">@import url("http://www.example.com/ie7_9_styles.css");</style>');
}
else if(MSIE9>-1){
document.write('<style type="text/css">@import url("http://www.example.com/ie7_styles.css");</style>');
document.write('<style type="text/css">@import url("http://www.example.com/ie7_9_styles.css");</style>');
}
else if(MSIE10>-1){
document.write('<style type="text/css">@import url("http://www.example.com/ie7_styles.css");</style>');
document.write('<style type="text/css">@import url("http://www.example.com/ie7_9_styles.css");</style>');
}
else {
document.write('<link rel="stylesheet" href="http://www.example.com/nn_styles.css" type="text/css">');
document.write('<style type="text/css">@import url("http://www.example.com/ch_styles.css");</style>');
}
  • 写回答

4条回答 默认 最新

  • duanhe0817825 2013-03-04 07:58
    关注

    You may need to review your HTML markup if you really need a stylesheet for every browser. But to answer your question this is how I would do it. There may be some errors, I didn't test it.

    PHP

    $user_agents = array(
        'Opera' => 'nn',
        'Gecko' => 'nn',
        'MSIE 6.0' => 'ie'
    );
    
    $style_sheet = 'ie7'; // default
    foreach ($user_agents as $agent => $sheet) {
        if (strStr(getEnv('HTTP_USER_AGENT'), $agent)) {
            $style_sheet = $sheet;
            break;
        }
    }
    
    echo '<link rel="stylesheet" href="http://www.example.com/'.$style_sheet.'_style.css" type="text/css" media="screen">';
    

    Javascript

    You should have a consistant naming convention for your style sheets to make it easier.

    var user_agents = ['MSIE 9.0', 'MSIE 6.0', 'MSIE', 'Gecko', 'Opera'],
        style_sheets = ['ie7_9', 'ie', 'ie7', 'nn', 'op'], // indexs correspond with user_agents array
        inserted = false;
    
    user_agents.forEach(function(item, index) {
        if (!inserted && navigator.userAgent.indexOf(item) > -1) {
            document.write('<link rel="stylesheet" href="http://www.example.com/'+style_sheets[index]+'_styles.css" type="text/css">');
            document.write('<style type="text/css">@import url("http://www.example.com/'+style_sheets[index]+'_styles.css");</style>');
            inserted = true;
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频