Positions in registerJsFile() not working

Yii 2.0.2

documentation: http://www.yiiframework.com/doc-2.0/yii-web-view.html#registerJsFile()-detail

this works, but inserts before jquery.js


$this->registerJsFile(Yii::$app->request->BaseUrl . '/js/custom.js');

this doesn’t work. no errors on page and custom.js is not added.


$this->registerJsFile(Yii::$app->request->BaseUrl . '/js/custom.js', ['position' => '\yii\web\View::POS_END']);

// or

$this->registerJsFile(Yii::$app->request->BaseUrl . '/js/custom.js', ['position' => \yii\web\View::POS_END]);

// or

$this->registerJsFile(Yii::$app->request->BaseUrl . '/js/custom.js', ['position' => $this::POS_END]);

// or

$this->registerJsFile(Yii::$app->request->BaseUrl . '/js/custom.js', ['position' => 3]);

what’s a matter? thx

I think you misunderstood the position parameter. This is the position relative to the html template.

Take a look at your template, you will find $this->head()

$this->registerJsFile support the following position:

  • POS_HEAD: in the head section [$this->head()]
  • POS_BEGIN: at the beginning of the body section [$this->beginBody()]
  • POS_END: at the end of the body section. This is the default value. [$this->endBody()]

Now if you need to include your javascript after jequery just use the following:


$this->registerJsFile(Yii::$app->request->BaseUrl . '/js/custom.js', ['depends' => [yii\web\JqueryAsset::className()]]);

Or better user @web alias


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


$this->registerJsFile(Yii::$app->request->BaseUrl . '/js/custom.js', ['depends' => [yii\web\JqueryAsset::className()]]);

thank you for reply. this works, but why POS_END doesn’t work?

I need to include it in my view template (not controller).

Yes since it is a view method.

What do you mean? It does not print the script or is not in the position you expect?