• 10
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Yii2 How to add parameters to script tag

The JS assests that gets renders looks like this:

<script src="/assets/a00ccd3f/jquery.min.js"></script>

I need to add the async="async" tag to it.

so it should look like

<script src="/assets/a00ccd3f/jquery.min.js" async="async" ></script>

How can i do this?

In your own AssetBundle you can add

public $jsOptions = [
    'async' => 'async',
];

If you want to add this to the Yii2 JqueryAsset bundle (or some other bundle) you can add it to the components parts of your config:

'assetManager' => [
    'bundles' => [
        'yii\web\JqueryAsset' => [
            'jsOptions' => [
                'async' => 'async'
            ],
        ],
    ],
],
  • 16
Reply Report

like this

<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace frontend\assets;
use yii\web\AssetBundle;
class ThemeCustomAsset extends AssetBundle
{
public $basePath    = '@webroot';
public $baseUrl     = '@web';
public $css = [];
public $js = [
    "js/lazysizes.min.js"
];
public $depends = [
    #'yii\web\YiiAsset',
    #'yii\bootstrap\BootstrapAsset',
];
public $jsOptions = ['async' => 'async'];
}
  • 0
Reply Report

Warm tip !!!

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

Trending Tags

Related Questions