Using your own jQuery file with Yii

(and have them play nice)

A common issue and one that I knew there was a Yii solution for but didn’t see clearly documented is how to include your own jQuery file, particularly on a site that has mixed pages where Yii includes it’s own version on some pages and not on others — which can result in jQuery loading twice and causing problems that are not always immediately obvious as to the cause.

In any case, this should do the trick. If you don’t need jQuery on every page, you can leave off or wrap the registerScriptFile line in a conditional and Yii will use your jQuery file when it needs it.

$cs=Yii::app()->clientScript;
$cs->registerScriptFile(Yii::app()->getBaseUrl() . '/js/libs/jquery.min.js', CClientScript::POS_HEAD);
$cs->scriptMap=array(
    'jquery.js'=>Yii::app()->getBaseUrl() . '/js/libs/jquery.min.js',
);

Leave a Reply