rockstardev
rockstardev

Reputation: 13527

How to add javascript to YII correctly?

I want to create several javascript function that will be needed on different pages. Most will be relevant only to one page, but some to several. I know if I add general conversion functions, it would be a good idea to just create a new javascript file and put all these generic functions into that one file. Bringing me to my first question:

Then, I need to address the placement of other javascript code.

The emphasis is on doing it correctly. I want to fall exactly in line with the yii framework.

Upvotes: 1

Views: 2853

Answers (2)

Nikos Tsirakis
Nikos Tsirakis

Reputation: 739

The best way to have you generic js code into /js/ or similar named folder under root of your app code. Personally I would separate my custom code files into one other subdirectory /js/custom/ and /js/vendors/ where in this vendor folder you can put ready js code such as jquery plugins etc.

Also don't forget to set this path to config file like this:

'components'=>array(

   'clientScript' => array(

      'coreScriptUrl' => 'path/to/js/lib/dir',

      'enableJavaScript' => true,

   ),

),

where path/to/js/lib/dir is your final js folder name path

Upvotes: 0

hemc4
hemc4

Reputation: 1633

store your generic javascript file in your_app/js folder i.e js folder is at same level to protected. if js is only used on one page than it will be better not to use generic file.

Upvotes: 3

Related Questions