Answer

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

  • 10
Reply Report

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]); 
  • 4
Reply Report
      • 2
    • registerJsFile syntax is incorrect because second parameter is array like this $this->registerJsFile('@frontend_base/web/js/sendverification.js', [ 'position' => yiiwebView::POS_END, ]);

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.

  • 0
Reply Report
      • 2
    • the answer above by @Sammaye also does the same thing it isnt necessary to add the js to an asset file and make it load on every page where it isnt required you can add the "depends" =>"yiiwebJqueryAsset" with in the registerJsFile param too and it will load after the jquery assets have been loaded.
    • If you add the asset bundle to the layout it will appear on every page. If you add the asset bundle to the view file it will just appear in that view. Typically in production systems all the javascript (and css) is bundled together and compress into 1 file (for each). While this can include all the javascript in every page, the footprint is much smaller and there is less requests made on the server. AssetBundles help you organise all this in lots of different ways without changing any of your view files. AssetBundles are Yii's recommended way of doing it.
    • Yes you are right, but the compression is manual not by assetbundle, what I was saying is that you don't have to add one file to asset bundle in order to add a dependency on the jqueryAsset or any other , what is the point of creating an asset bundle for one file alone to be included in a single view, and obviously no point of compressing it,. you could still include that file inside view and add dependency to jquery assets, good day

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions