giovaZ
giovaZ

Reputation: 1470

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

Upvotes: 15

Views: 11047

Answers (1)

soju
soju

Reputation: 25312

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.

Upvotes: 21

Related Questions