停止从Yii中的assets文件夹加载jquery库

时间:2022-06-16 00:26:03

I had problem in my app that made it slow , after checked firebug I noted that jquery-ui loaded twice first from google.com and second from assets folder ("232kb") .

在我的应用程序中遇到问题导致它变慢,经过检查后的firebug我注意到jquery-ui首先从google.com加载两次,第二次从assets文件夹(“232kb”)加载。

How to force it to load from google.com without assets version ?

如何强制它从没有资产版本的google.com加载?

停止从Yii中的assets文件夹加载jquery库

View content " JavaScript to call Ajax function ":

查看内容“调用Ajax函数的JavaScript”:

....
     $(".third,#second-next,#fourth-pr").click(function () {

                $.ajax({
                    url: '<?php echo Yii::app()->createUrl('site/CallScientificForm',array('language'=>language())); ?>',
                    type: 'GET',
                    dataType: 'html',
                    beforeSend: function () {
                        $("#loading").show();
                    },
                    success: function (data, textStatus, xhr) {
                        $("#hr3").css("background", "#51a351");
                        $("*").removeClass("active");
                        $(".third").addClass("active");
                        $("#firstContent ,#secondContent,#thirdContent,#fourthContent").fadeOut(2000);
                        $("#thirdContent").html(data);
                        $("#thirdContent").fadeIn(2000);
                        $("#loading").hide();
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        $('#' + id + ' .contentarea').html(textStatus);
                    },
                    complete: function() {
                        $(this).data('requestRunning', false);
                    }
                });

            });
...

Controller:

控制器:

...
  public function actionCallScientificForm()
    {
        $scienceModel = new MembershipScientific();
        $view= $this->renderPartial('_ScientificForm', array('scienceModel' => $scienceModel, 'language' => language()), false, true);
        echo $view;
        Yii::app()->end();
    }
..

Main config:

主配置:

 'clientScript'=>array(
            'packages'=>array(
                'jquery'=>array(
                    'baseUrl'=>'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/',
                    'js'=>array('jquery.js'),
                    'coreScriptPosition'=>CClientScript::POS_END
                ),
                'jqueryMin'=>array(
                    'baseUrl'=>'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/',
                    'js'=>array('jquery.min.js'),
                    'coreScriptPosition'=>CClientScript::POS_END
                ),
                'jquery-ui'=>array(
                    'baseUrl'=>'https://code.jquery.com/ui/1.11.1/jquery-ui.min.js',
                    'js'=>array('jquery-ui.min.js'),
                    'depends'=>array('jquery'),
                    'coreScriptPosition'=>CClientScript::POS_END
                )
            ),
        ),

And I called it in target view like this :

我在目标视图中调用它,如下所示:

....
cs = Yii::app()->getClientScript();

$cs->registerCoreScript('jquery');
$cs->registerCoreScript('jquery-ui');
....

2 个解决方案

#1


3  

Set them in clientScript to false to prevent them from loading.

将它们在clientScript中设置为false以防止它们加载。

A few examples of scripts you can disable:

您可以禁用的一些脚本示例:

'components' => array(
    'clientScript' => array(

        // disable default yii scripts
        'scriptMap' => array(
            'jquery.js'     => false,
            'jquery.min.js' => false,
            'core.css'      => false,
            'styles.css'    => false,
            'pager.css'     => false,
            'default.css'   => false,
        ),
    ),

#2


1  

Use script map:

使用脚本映射:

'clientScript' => [
    'class' => 'CClientScript',
    'scriptMap' => [
         'jquery.js' => '//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js',
         'jquery.min.js' => '//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js',
         'jquery-ui.js' => '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js',
         'jquery-ui.min.js' => '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js'
    ],
....

Also remove jQuery and jQuery ui from packages. This should resolve double registering.

还从包中删除jQuery和jQuery ui。这应解决双重注册问题。

NOTE: It is better to use protocol-relative urls: //example.com/blah/ - this way will work also with https

注意:最好使用协议相对URL://example.com/blah/ - 这种方式也适用于https

#1


3  

Set them in clientScript to false to prevent them from loading.

将它们在clientScript中设置为false以防止它们加载。

A few examples of scripts you can disable:

您可以禁用的一些脚本示例:

'components' => array(
    'clientScript' => array(

        // disable default yii scripts
        'scriptMap' => array(
            'jquery.js'     => false,
            'jquery.min.js' => false,
            'core.css'      => false,
            'styles.css'    => false,
            'pager.css'     => false,
            'default.css'   => false,
        ),
    ),

#2


1  

Use script map:

使用脚本映射:

'clientScript' => [
    'class' => 'CClientScript',
    'scriptMap' => [
         'jquery.js' => '//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js',
         'jquery.min.js' => '//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js',
         'jquery-ui.js' => '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js',
         'jquery-ui.min.js' => '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js'
    ],
....

Also remove jQuery and jQuery ui from packages. This should resolve double registering.

还从包中删除jQuery和jQuery ui。这应解决双重注册问题。

NOTE: It is better to use protocol-relative urls: //example.com/blah/ - this way will work also with https

注意:最好使用协议相对URL://example.com/blah/ - 这种方式也适用于https