Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 loading Jquery in the head of the page

In my Yii2 application i've a script that need Jquery to be loaded in the head of the page.

I know there's a parameter that can be setted inside AppAssets.php :

public $jsOptions = [
    'position' => \yii\web\View::POS_HEAD
];

but this will render all the Javascripts Files on the head of the page. Is possibile to load only Jquery on the head?

Thanks in advance for all the help

like image 408
giovaZ Avatar asked Mar 02 '16 12:03

giovaZ


1 Answers

You could simply do this in your view, e.g. :

$this->registerAssetBundle(yii\web\JqueryAsset::className(), View::POS_HEAD);

Or if you want to do this for all your page, you could customize JqueryAsset in your components configuration :

'components' => [
    'assetManager' => [
        'bundles' => [
            'yii\web\JqueryAsset' => [
                'jsOptions' => [ 'position' => \yii\web\View::POS_HEAD ],
            ],
        ],
    ],
],

Read more about Customizing Asset Bundles.

like image 184
soju Avatar answered Sep 17 '22 11:09

soju