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

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度