registerJsFile POS_READY not works

Hello, I’m trying to include JS file to my View but when I use registerJsFile with POS_READY


$this->registerJsFile('@web/js/daily.js' , ['position' => View::POS_READY]);

JS code is not working, file daily.js is not included anywhere in generated web page source code. It is the same situation when I included JS file in Asset bundle.

But when POS_READY is changed to POS_BEGIN, JS file is included in generated web page source code and it’s working.

For testing purposes I have used following JS file:

daily.js


jQuery(document).ready(function() {

$('#myButton').on('click', function() { alert('Button clicked!'); });

});

What I’m doing wrong ? Please, could someone explain how to properly use custom jQuery code in Yii? Thank you in advance

You cannot register a JS file with POS_READY position. POS_READY is only used for JS code blocks with "registerJs"

Hi,

Your js Code uses jQuery, so you may do like this




$this->registerJsFile('@web/js/daily.js', ['depends' => 'jquery']);



By default, after registering the jquery library your script file will be registered so no problem in executing your scripts.

Thanks for your answers. jQuery code is now working, even included throught asset file. Only thing I have to change is depends property:


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