How to override main jquery file with external hosted jquery from google

In Yii, all jquery dependencies currently load the local version of jquery, which in my opinion is 1.6. *, un-minimified.

Something along the lines of:

<script src="/assets/2343/js/jquery.js"></script> 

I would like to update the jQuery kernel dependency on using jquery 1.7. * from google

Mostly I would like to include

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"> 

at the bottom of all my pages when jquery is a dependency.

+6
source share
4 answers

There is also another method specified in yii docs :

For example, we can include jquery.js from Google servers instead of our own server. To do this, first configure scriptMap as follows:

 $cs=Yii::app()->clientScript; $cs->scriptMap=array( 'jquery.js'=>false, 'jquery.ajaxqueue.js'=>false, 'jquery.metadata.js'=>false, ...... ); 

By matching these script files to false, we prevent Yii from generating code to include these files. Instead, we write the following code on our pages to explicitly include script files from Google,

 <head> <?php echo CGoogleApi::init(); ?> <?php echo CHtml::script( CGoogleApi::load('jquery','1.3.2') . "\n" . CGoogleApi::load('jquery.ajaxqueue.js') . "\n" . CGoogleApi::load('jquery.metadata.js') ); ?> ...... </head> 
+14
source

After a bit more searching and searching, I found the answer:

in the configuration, in the "Components" section

 'clientScript'=>array( 'packages'=>array( 'jquery'=>array( 'baseUrl'=>'http://ajax.googleapis.com/ajax/libs/jquery/', 'js'=>array('1.7.2/jquery.min.js'), ) ), ), ), 
+25
source

Google's first hit: http://www.yiiframework.com/wiki/259/serve-jquery-and-jquery-ui-from-google-s-cdn/

Next to overriding the client interface configuration, you can also override the widgetFactory configuration:

  <?php return array( // other config 'components'=>array( 'clientScript'=>array( 'packages'=>array( 'jquery'=>array( 'baseUrl'=>'//ajax.googleapis.com/ajax/libs/jquery/1/', 'js'=>array('jquery.min.js'), ) ), // other clientScript config ), 'widgetFactory'=>array( 'widgets'=>array( 'CJui<WidgetName>'=>array( // where <WidgetName> is the name of the JUI Widget (Tabs, DatePicker, etc.). Each CJuiWidget used must be declared 'scriptUrl'=>'//ajax.googleapis.com/ajax/libs/jqueryui/1/', 'theme'=>JUI-THEME, 'themeUrl'=>'//ajax.googleapis.com/ajax/libs/jqueryui/1/themes/', ), // Repeat for other CJuiWidgets ), ), // other component config ), // other config ); 
+2
source

I know this stream is quite old. But because of what I just received, I would have thought it would be important to mention this.

Somewhere in the latest versions of Yii, I quickly switched from 1.1.11 to the current one, the package mechanism was not only implemented, but also improved. It existed earlier, but now it actually got apoint, where the previously declared and checked way to set the jQuery part to false no longer worked. Although, I found out how to fix it!

 $cs->packages["jquery"] = [ "basePath"=>Yii::app()->cdn->basePath, "baseUrl"=>Yii::app()->cdn->baseUrl, "js"=>["js/jquery-1.11.1.js"] ]; 

This is taken directly from my code, but illustrates how I did it. Basically, I added a jQuery entry to the package list. When the main scenarios are decided, this list is scanned before the actual CoreScripts. This way it will be picked up first and you can add your personal jQuery version this way.

Hope this helps!

0
source

Source: https://habr.com/ru/post/915282/


All Articles