How Manage *.js/*.css Files Of External Extentions

Hi,

In my yii 1.1.14 app I use some .js/.css files and I put them in header of layout, which is common for all app.

Also I use some external extentions and I see that these extentions set their own .js/.css files and they are put into the same layout and they are above my .js/.css files.

Seems that is not best way, as these extentions file are somethimes of lower versions then my files.

What is the tge best way of managing my own .js/.css files and files of external extentions ?

Also in source of page I see links like local-yii-tyb.com/tybapp/assets/995e17c6/jquery.js.

How can I define which external extention added this file ?

Thanks!

use CClientScript to manage scripts (js and css). you can configure mapping of scripts used by framework/components to your own files which should give you proper version across application.assets are hard to track, but this is rather simple - it is Yii jquery (added by some component using Yii::app()->clientScript->registerPackage( ‘jquery’ ); )

read more about mapping and clientscript:

http://www.yiiframework.com/wiki/572/how-to-include-javascript-css

http://www.yiiframework.com/doc/guide/1.1/en/topics.performance#minimizing-script-files

http://www.yiiframework.com/doc/api/1.1/CClientScript (especially param scriptMap)

Thank you, that was helpfull!

Also there is a question that in source of my page I see lines like:


<script type="text/javascript" src="/local-yii-tyb.com/tybapp/assets/995e17c6/jquery.js"></script>

<script type="text/javascript" src="/local-yii-tyb.com/tybapp/assets/995e17c6/jquery.yiiactiveform.js"></script>

<script type="text/javascript" src="/local-yii-tyb.com/tybapp/assets/mine/jquery/jquery-1.9.1.js"></script>



I think that first jquery.js was set by some extension.

And first jquery.js is of lower version of jquery-1.9.1.js which I use and I am afraid that in some cases that could raise problems.

What is the proper way to cope it ?

use scriptMap and map jquery.js to your file location.

http://www.yiiframework.com/forum/index.php/topic/9070-use-own-jquery-script-files-instead-of-yiis/

Thank you for reply, but seems I still do not uderstand how to use scriptMap

In beforeAction action of my projects controll /protected/components/Controller.php I wrote like :


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

$cs->registerPackage('jquery');

$cs->registerScriptFile($frontend_root_url . 'assets/mine/jquery/jquery-1.9.1.js');

$cs->registerScriptFile($frontend_root_url . 'assets/mine/jquery/jquery.yiiactiveform.js');

$cs->registerScriptFile($frontend_root_url . 'assets/mine/jquery/jquery-ui-1.10.1.custom.js');



and in source of my page I see :


<script type="text/javascript" src="/local-yii-tyb.com/tybapp/assets/c0740966/jquery.js"></script> // Reference to lower version that I do

<script type="text/javascript" src="/local-yii-tyb.com/tybapp/assets/c0740966/jquery.yiiactiveform.js"></script>

<script type="text/javascript" src="http://localhost/local-yii-tyb.com/tybapp/assets/mine/jquery/jquery-1.9.1.js"></script>

<script type="text/javascript" src="http://localhost/local-yii-tyb.com/tybapp/assets/mine/jquery/jquery.yiiactiveform.js"></script> // jquery.yiiactiveform.js is included twice




I tried to write something like :


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

$cs->registerPackage('jquery');

$cs->scriptMap = array(

	'jquery.js' => false,

'jquery.yiiactiveform.js' => false,

);

$cs->registerScriptFile($frontend_root_url . 'assets/mine/jquery/jquery-1.9.1.js');

$cs->registerScriptFile($frontend_root_url . 'assets/mine/jquery/jquery.yiiactiveform.js');

$cs->registerScriptFile($frontend_root_url . 'assets/mine/jquery/jquery-ui-1.10.1.custom.js');



But after that in console I get errors that jquery or yiiactiveform is not defined. What is the right way ?

define scriptMap in config file. Put your jquery in [WWWROOT]/js/jquery-1.9.1.js




'components'=>array(

   ...

   'clientScript'=>array(

      ...

      'scriptMap'=>array(

         'jquery.js'=>'/js/jquery-1.9.1.js',

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

      ),

      ...

   ),

...



of course you need $frontend_root_url defined. you may also map jquery.js -> false in config scriptMap and put your own jquery in template file but it must be loaded as first resource…