Use External JavaScript file in yii2

The arguments for registerJsFile() are similar to those for registerCssFile(). In the following example, we register the main.js file with the dependency on the yii\web\JqueryAsset. It means that the main.js file will be added AFTER jquery.js.

An external script can be added like the following:

Example (code should be inserted at bottom of your view)

$this->registerJsFile(
    '@web/js/main.js',
    ['depends' => [\yii\web\JqueryAsset::className()]]
);

Where main.js should be in your web/js folder.

Share

You may also like...