duancuan6466 2014-02-24 18:18
浏览 44
已采纳

Yii - 如何根据浏览器注册脚本?

Does someone know a clean way to set different versions of jQuery, depending on browser, with an integrated use of Yii? In my case, I want to load jQuery 2 to all browsers except to IE 8-.

I'm using, in config/main.php, this clientScript:

    'clientScript'=>array(
            'packages'=>array(
                'jquery'=>array(
                    'baseUrl'=>'//ajax.googleapis.com/ajax/libs/jquery/2.0.3/',
                    'js'=>array('jquery.min.js'),
                ),
            )
    ),

Yii 1.1.14 carries jQuery 1.8.3, and it would get referenced into the page when calling registerCoreScript():

$cs = Yii::app()->clientScript;
$cs->registerCoreScript('jquery.ui');

, because jQuery is registered as a dependence of jQueryUI. But it doesn't, because of my clientScript, defined as above. Hence, all browsers get jQuery 2 and the problem is that it doesn't offer support to IE 8-.

There is also the option of using IE comments, just like:

<!--[if (gte IE 6)&(lte IE 8)]>

So, here is the question: I would like to avoid the solution with comments and let the Yii do the job for me, but how?

  • 写回答

1条回答 默认 最新

  • dsklfsdlkf1232 2014-02-25 10:07
    关注

    You can use CHttpRequest::getBrowser()

    $browserInfo=Yii::app()->request->getBrowser();
    if (($browser['Browser']=="MSIE")&&(floatval($browser['version'])<9)){
    // register legacy scripts
    }else{
    //register normal scripts
    }
    

    In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system.

    But the best decision can be - not to support IE 9 and older

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大