Answer the question
In order to leave comments, you need to log in
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);
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',
];
}
<script type="text/javascript">jQuery(function ($) {
$(window).on('load', function () {
$('.container').append('<p>Show!!!</p>')
});
});</script></body>
<script type="text/javascript">jQuery(function ($) {
$('.container').append('<p>Show!!!</p>')
});</script></body>
Answer the question
In order to leave comments, you need to log in
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);
}
}
POS_END
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question