Use own jQuery script files instead of Yii's

Yii seems to auto load jquery.js, jquery.ba-bbq.js and jquery.yiilistview.js; but I already included my own jQuery library and custom scripts in my main.php layout file. How can I stop Yii from loading it’s version of jQuery script files?

Take a look here: http://www.yiiframework.com/doc/guide/topics.performance#minimizing-script-files

Cheers. I did:


<?php $cs=Yii::app()->clientScript;

$cs->scriptMap=array(

    'jquery.js'=>false,

); ?>

This works, but then I thought it would be a good idea to include my existing script files within that same script map, but I had no luck in doing that. It did not seem to output the script files on page load. Here is a sample code:


<?php $cs=Yii::app()->clientScript;

$cs->scriptMap=array(

    'jquery-1.4.2.min.js'=>'/js/jquery-1.4.2.min.js',

    'jquery-ui-1.8.custom.min.js'=>'/js/jquery-ui-1.8.custom.min.js',

    'jquery.js'=>false,

); ?>

1 Like

You still need to register the script files as:




$cs->registerScriptFile('jquery-1.4.2.min.js',CClientScript::POS_HEAD);

$cs->registerScriptFile('jquery-ui-1.8.custom.min.js',CClientScript::POS_HEAD);



The only problem is that jQuery will be inserted after some other js files like jquery.yiiactiveform.js so you can do smth like this for jQuery




<?php $cs=Yii::app()->clientScript;

$cs->scriptMap=array(

    'jquery.js'=>'/js/jquery-1.4.2.min.js',

    'jquery-ui-1.8.custom.min.js'=>'/js/jquery-ui-1.8.custom.min.js',

); 

$cs->registerScriptFile('jquery-ui-1.8.custom.min.js',CClientScript::POS_HEAD);

?>

I’m a bit confused regarding registerScriptFile() function. I have the scripts in my main.php layout file so I just use CHtml::scriptFile() to include them. I assumed registerScriptFile() was for including script files in views?

CHtml::scriptFile() generates the script tag for the desired URL, but it does not use the scriptMap feature…

so if you wan’t to use the scriptMap for your custom scripts than use registerScriptFile()

the later is better because you don’t need to have it in the main.php but only in the views that you need it, so it would not be included everytime.

You just simply add this steps to add [size="3"]external Javascript in yii [/size],

step : 1

make folder name js in your project folder if not there ,

put your custom javascript there like formValidation.js

step : 2

include following code in view file ,

<?php

$baseUrl = Yii::app()->baseUrl;

$cs = Yii::app()->getClientScript();

$cs->registerScriptFile($baseUrl.’/js/formValidation.js’);

?>

…finish nothing else.

[size="3"][u][b]It works 100%

If helpful give user rating to my profile.[/b][/u][/size]