S
S
Sergey Burduzha2017-10-12 13:01:26
Yii
Sergey Burduzha, 2017-10-12 13:01:26

Why doesn't jquery.onload work in yii2?

Registered a block of code in view

$this->registerJs("$('.container').append('<p>Show!!!</p>')", \yii\web\View::POS_LOAD);

Here are the settings from the AppAsset class
class AppAsset extends AssetBundle
{
  public $basePath = '@webroot';
  public $baseUrl = '@web';
  public $css = [
    'css/site.css',
    //'css/style.css'
  ];
  public $js = [
    //'js/scripts.js'
  ];
  public $jsOptions = ['position' => View::POS_END];
  public $depends = [
    'yii\web\YiiAsset',
    'yii\bootstrap\BootstrapAsset',
  ];
}

Here is the result from source code
<script type="text/javascript">jQuery(function ($) {
$(window).on('load', function () {
$('.container').append('<p>Show!!!</p>')
});
});</script></body>

but without the POS_LOAD option
<script type="text/javascript">jQuery(function ($) {
$('.container').append('<p>Show!!!</p>')
});</script></body>

In the second case, JQuery(function) shouldn't have been there.
I will be grateful for help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kim, 2017-11-21
@kimono

So from the docks, after all:

public function registerJs($js, $position = self::POS_READY, $key = null)
    {
        $key = $key ?: md5($js);
        $this->js[$position][$key] = $js;
        if ($position === self::POS_READY || $position === self::POS_LOAD) {
            JqueryAsset::register($this);
        }
    }

TryPOS_END

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question