I have some query like how to include js file in last position after jquery file includes. beacuse whats the problem right now is my js load first and after that my jQuery file loads. like right now i have added file like:
use frontend\assets\AppAsset;
AppAsset::register($this);
$this->registerJsFile('@frontend_base/web/js/sendverification.js');
so this will add js file but not in last. so how i can achive this?
The recommended solution is to use AssetBundles. Something like:
class SendverificationAsset extends AssetBundle {
public $basePath = '@webroot';
public $baseUrl = '@web';
public $js = [
'js/sendverification.js'
];
public $depends = [
'yii\web\JqueryAsset'
];
}
Then in your view file:
use frontend\assets\AppAsset;
use frontend\assets\SendverificationAsset;
AppAsset::register($this);
SendverificationAsset::register($this);
The dependency will make sure that SendverificationAsset
's Javascript and/or CSS always come after whatever JQuery includes.
Using AssetBundles also gives you the advantage of easily being able to minify/compress your code for production use.
You can actually make your script have a dependency on jQuery. This is the only real way, without using inclusion pattern oddities, to ensure this:
$this->registerJsFile('@frontend_base/web/js/sendverification.js', ['depends' => 'yii\web\JqueryAsset'])
Using the POS_
constants only ensure that the JS is included at a certain part of the page, not after a certain file and can be included before that file.
You can read more here: http://www.yiiframework.com/doc-2.0/yii-web-view.html#registerJsFile()-detail
You can define position of Js File as Register Js File
$this->registerJsFile('@frontend_base/web/js/sendverification.js',['position' => \yii\web\View::POS_END]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With