Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top