Yii Framework Forum

assets and jQery


(Baender3002) #1

Hi,

I’m doing my first steps on Yii2 an playing around with the example site from the Yii2 Basic environment.

Here I found out, that jQuery is loaded automatically, because AppAssets are registered in the main.php-View.

In my use case, jQuery will be loaded at the website from an 3rd party template, which is not under Yii Control.

Because of the


AppAsset::register($this)

, jQuery is loaded twice, which will overwirte the first loaded jQuery and break all the existing bindings.

So my Question is: is there a way to use AppAssets but avoid loading jQuery a 2nd time?

Thank you,

Borner


(Yiiframework) #2

Your AppAsset depends on YiiAsset which in turn depends on JqueryAsset.

You could remove the dependency to YiiAsset and just manually add its content to your AppAsset. But I don’t know if that’s the best approach.


(Wadeshuler) #3

Edit your apps config:




return [

    // ...

    'components' => [

        'assetManager' => [

            'bundles' => [

                'yii\web\JqueryAsset' => [

                    'sourcePath' => null,   // do not publish the bundle

                    'js' => [

                        '//code.jquery.com/jquery-2.2.4.min.js',

                    ]

                ],

            ],

        ],

    ],

];



That will load jquery from the official CDN. The double ‘//’ lets it work on both http and https, without throwing an insecure content error. Its common to do this when using CDNs. Instead of the CDN url, you could use the URL pointing to your copy wherever it is… ie: myothersite.com/js/jquery.min.js

I personally always use the CDN. I never modify the original library. This makes it easier to upgrade in the future.


(Baender3002) #4

It’s not my intention to load an different jQuery version. My goal is to disable jQuery, because this library is loaded from an dedicated part of the website, which is not created by the Yii2 application.

So, the following part of your answer:

…should solve my problem, right?