How to add JavaScript to you view files using the Yii Framework

H

Add the JavaScript code as below to your view

	$js = Yii::app()->getClientScript();
 
	$js->registerScript(
 
	'my-javascript-id',
 
	'var value = "Hello World";
 
	alert(value);',
 
	CClientScript::POS_END );

Yii’s CClientScript is used to place JavaScript to your views.

The arguments of register script are as follows:

  • The first argument uniquely identifies your script
  • The second argument contains your actual JavaScript
  • The third argument sets the position where the JavaScript should be placed. In the example above the CClientScript::POS_END specifies that the script will be placed before the closing </body> tag.

More details about registerScript can be found here

About the author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

About Author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

Follow Me