How to use client script

Hey there,

can someone provide a simple example of how to use the clientScript class please.

I'd like to have the javascript inserted just before the end body tag and also would like to register specific files for each controller…as opposed to every page loading every file… sure this is possible but can't quite see how to do it.

Cheers :)


Check CClientScript API form detailed information of various register methods.

I've been troubling with that too and still are, here's a link to some talk about it:


Qiang, can you shed some light on how to push those being included in a view beneath the ones in a layout? :)

Could you describe what exactly is your problem?

My problem is that i would normally have a bunch of css and js files included as default. That is i will have them included in my layout file.

However in one view i might want to include some js file specific to that page. Currently i solve this by using CHtml and have them included in my body.

But i would rather want them included in my <head> part just like when you include them with ClientScript. However if i register a css or js in a view it will end up being included above the css and js in my layout file.

So if i for example have mootools included in my layout and have datepicker written in mootools that i would like to use in one of my views it would be included on top of the mootools inclusion and i will end up with JS errors because of that.

My explanation might be a bit confusing, if you like i can give you a html example or do you get my point? :)

I guess the easiest way of solving this type of problem would probably be to add the option to specify the index key of the file i include. But maybe there is a option already? And i just haven’t stumbled upon it yet? :)



There's a trick here: if you use registerScriptFile() to insert a script in the head section, the script will be inserted right before the <title> tag. So you can move around your other scripts in the layout so that the inserted script appears in the expected order.

Im haveing the same problem, and moving around myscripts in the layout doesnt help if I use CClientScript::registerScriptFile(). The only way i get it to work its using rather <script src=""> or CHtml::scriptFile(); which I dont really like if im supposed to use CClientScript::registerScriptFile() which would be the "good way".

Is there any other option or is this the only way around?

Create a function in your CONTROLLER:

    protected function publishJsCss() {

        // publish CSS or JavaScript file here...

        $cssUrl = Yii::app()->getAssetManager()->publish('protected/controllers/YOURFOLDER/YOURFILEController.css');

        $jsUrl  = Yii::app()->getAssetManager()->publish('protected/controllers/YOURFOLDER/YOURFILEController.js');

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





In your ACTION (is need publish the files) use this:





           |-> YOURFILEController.css   (your file stylesheet)

           |-> YOURFILEController.js    (your file javascript)

           |-> YOURFILEController.php   (your file controller)

The files has the same name, only change the extension.

This way the files will be together, being very easy of finding them.

Very simple! :D