Why Use Yii::app()->Clientscript->Registerscript?

been wondering what is the difference in using:


Yii::app()->clientScript->registerScript('scriptname',"//scripts...");

from:


<script type="text/javascript">//scripts...</script>

any Pros and Cons?

Pros, you can control when to load the scripts. See https://github.com/yiisoft/yii/blob/1.1.15/framework/web/CClientScript.php.

You can register scripts as many times as you want (each time an component or an extension requires it) and it will still be generated only 1 time in your HTML.

Also you can post process registered scripts by extending CClientScript, just like the extension that I just published: http://www.yiiframework.com/extension/timestampclientscript/ .

thank you for the idea, would you care to give an example? if i register a scrip on a viewpage, say:

post/view/1


Yii::app()->clientScript->registerScript('MyJScript',"//scripts...");

can you give a good example of its benefits/usage, please, appreciate it.

thank you for the idea.