Yii conflict with jQueryTools

I need to use jQueryTool on my project together with ajaxLink and ajaxSubmitButton.

But seem that jQueryTool does not work. I realize that if I comment out


Yii::app()->getClientScript()->registerCoreScript('jquery');

line in CHtml then jQueryTool will work.

This error occurs even if I use registerScript with position is POS_READY or POS_LOAD, for this if I comment out these two lines:




//if($position===self::POS_READY || $position===self::POS_LOAD)

			//$this->registerCoreScript('jquery');



then jQueryTool will work.

This error is for all tasks that Yii auto register core script.

I also updated jQuery in framework to latest version but does not help.

Hope anyone can find out what is problem here.

Thanks.

Take a look at your source code. Do you load jQuery twice? Do you work with other js frameworks like mootools? You should show us the url of your site.

I do not use any additional js framework and jQuery is only loaded once. This project is private so could not be posted here.

I have just realize that Fancybox also does not work. If I remove all CHtml::ajaxLink and CHtml::ajaxSubmitButton then they would work.

P/S: I am using the latest version of Yii.

It’s really hard to help you without seeing some pieces of your code… Could you at least copy the html source code and post it here?




<head>

        <meta charset="utf-8">

        <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/template_xenforo.css" />

        <?php Yii::app()->getClientScript()->registerCoreScript('jquery'); ?>

        <script type="text/javascript" src="<?php echo Yii::app()->theme->baseUrl; ?>/js/jquery.tools.min.js"></script>

        <title><?php echo CHtml::encode($this->pageTitle); ?></title>

    </head>






<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/jquery.fancybox.css" />

<script type="text/javascript" src="<?php echo Yii::app()->theme->baseUrl; ?>/js/jquery.fancybox.pack.js"></script>

<script type="text/javascript">

    jQuery(function($){

        $('.messageContent img').each(function(){

            var width = $(this).width();

            var height = $(this).height();

            if (width > 400)

            {

                $(this).css('width', '400px').css('height', height*(400/width)+'px');

                $(this).parent().html('<a class="fancybox" href="'+$(this).attr('src')+'">'+$(this).parent().html()+'</a>');

            }

        });

        console.dir($.tools);

        $('.fancybox').fancybox();

    });

</script>



I don’t understand, so why would you register jQuery yourself? Yii will do it for you (when you use POS_READY or POS_LOAD) unless you set scriptMap accordingly.

jQueryTools by default contains the jQuery… so if Yii registers jQuery you are getting jQuery included for 2 times…

You have 2 options… set Yii to not load jQuery by using scriptmap… or create a custom jQueryTools that do not contain jQuery ( http://www.jquerytools.org/download/ )

I prefer the second option as this way you can choose only the features you need, this way you make the ending jQueryTools file much smaller.

You must rearrange the order of your elements in the head section. Try this:




<?php Yii::app()->getClientScript()->registerCoreScript('jquery'); ?>

...

doctype

html tag

...

<head>

   <meta charset="utf-8">

   <title><?php echo CHtml::encode($this->pageTitle); ?></title>

   <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/template_xenforo.css" />

   <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/jquery.fancybox.css" />

   <script type="text/javascript" src="<?php echo Yii::app()->theme->baseUrl; ?>/js/jquery.fancybox.pack.js"></script>

</head>



Otherwise yii inserts your jquery file AFTER your fancybox and custom script.

I have fixed this issue by using following head section




    <head>

        <meta charset="utf-8">

        <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/template_xenforo.css" />

        <script type="text/javascript" src="<?php echo Yii::app()->theme->baseUrl; ?>/js/jquery.min.js"></script>

        <?php 

        $cs=Yii::app()->clientScript;

        $cs->scriptMap=array(

                'jquery.js'=>false,

        ); 

        ?>

        <script type="text/javascript" src="<?php echo Yii::app()->theme->baseUrl; ?>/js/jquery.tools.min.js"></script>

        <title><?php echo CHtml::encode($this->pageTitle); ?></title>

    </head>



I do not know why registerCoreScript made this problem (core jquery and my jquery are some because I duplicated them).

@bennouna: Because in head section I need to include jQueryTool, if jQuery not loaded then it would fail. Of course ajaxLink and ajaxButton still work in this case because they would include jquery themselves.

Thank for your help but this way also not work (I get "jQuery is not defined" in console for jQueryTools).

Please check my answer above. Do you think this is a bug?

It’s not a bug at all… check my answer above…

btw. the scriptmap you can set in the Yii configuration file…

Nope. I downloaded jQueryTools without jQuery. (Tried several options).

If your jQueryTools does not have jQuery… and you disable the Yii jQuery… then nothing would work… jQuery should be included for jQueryTools to work.

I have tried following.

  • Disable Yii jQuery + Include jQueryTools with jQuery.

  • Disable Yii jQuery + Include jQuery + Include jQueryTools without jQuery.

  • Enable Yii jQuery + Include jQueryTools without jQuery.

  • Enable Yii jQuery + Include jQueryTools with jQuery.

None of them work and I have no idea for the reason why.

Try and make it work without Yii first.

Does it work by itself?

Sounds stupid, but it’s worth trying.

@jacmore: Thank for your information. Today, after tried Fancybox extension and get the same error. I have realized that tinymce make this conflict (Even last time I tried to disable tinymce already)

I will have more try and will post updated information if the things work.

Good to hear that it was useful.

I often use elimination to pinpoint problem areas.