0

In yii2 I have put all my js code in 1 file using yii2-assets-auto-compress plugin, including jQuery lib. Loading this file is async to speed up page load. But, if there are forms on the page, yii adds yiiActiveForm() at the end of </body>. So, the error is jQuery is not defined.

How to manage this problem? Firstly, I can make call yiiActiveForm() manually from script.js, but how to turn it off automatically load at the end of the body? Generally, that's not convenient, because there might be other scripts that append js code. Maybe someone knows how append js code with this yii2-assets-auto-compress plugin?

<script src="/assets/js-compress/script.js?" async="async"></script>
<script type="text/javascript">
jQuery(document).ready(function () {
    jQuery('#w0').yiiActiveForm([],[]);
});
</script>
</body>
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
sirjay
  • 1,767
  • 3
  • 32
  • 52

1 Answers1

0

async downloads the file during HTML parsing and will pause the HTML parser to execute it when it has finished downloading.

  • You should either load Jquery outside your compressed scripts inside the head.
  • Or load the script inside the <head> of the document means inside the layout file either using script tags

    <script type="text/javascript" src="/path/to/compressed.js" async="async"></script>

    or using

    <?php $this->registerJsFile('/path/to/compressed.js',['aync'=>'aync'])?>

  • if you are using AssetManager to load the script file

Then load it like this

public $js = [
        ['js/compressed.js','async'=>'async','position' => yii\web\View::POS_HEAD],

    ];

Also, you should configure the asset manager to not load the core script jquery so that it does not load jquery twice.

'assetManager' => [
    'bundles' => [
          'yii\web\JqueryAsset' => [
              'sourcePath' => null, 
              'js'=>[]
           ],
     ],
 ],

HERE is a good read.

Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
  • Thank you for reply! I changed `async` to `defer`, but error was the same. It seems `defer` does not help. – sirjay Mar 17 '18 at 17:37
  • @sirjay can you provide the link to the script file you are using , so that i can test at my end , and you said you have included the jquery within that compressed file, have you disabled the corescript for jquery? overall the site make sure it isnt loading twice, and how are you including this file , using assets? – Muhammad Omer Aslam Mar 17 '18 at 17:51
  • Script that I use to compress is https://github.com/skeeks-semenov/yii2-assets-auto-compress, my website is https://arprime.ru to check error – sirjay Mar 17 '18 at 18:29
  • sorry i misinterpreted the logic here, sorry for that you can use `async` here but you need to load the file inside the ``, I have updated my answer @sirjay – Muhammad Omer Aslam Mar 17 '18 at 21:35
  • Hey! Unfortunately, it did not help. I put js load to `` and set `async`. That means it does not depend whether you put to `` or `` – sirjay Mar 22 '18 at 13:27