Problem with registerCoreScript

Hi

I’m using jQuery Simple Fade SlideShow (http://www.simplefadeslideshow.com) on a project, but I have a problem with the place where jQuery is loaded with registerCoreScript.

My code is in <head> and is this:


<?php

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


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

$cs->registerScriptFile(Yii::app()->baseUrl . '/js/FadeSlideShow/fadeSlideShow-minified.js', CClientScript::POS_HEAD);

?>

	<script type="text/javascript">

	jQuery(document).ready(function(){

			jQuery('#slideshow').fadeSlideShow({

				interval: 8000,

        width: 960,

        height: 428,

				PlayPauseElement: false,

				NextElement: false,

				PrevElement: false,

				ListElement: false

		});

		});

	</script>



The problem is that jquery and fadeSlideShow are being loaded after the function and I don’t know why. And because of that, when the function is called, it does not “know” jquery at that time.


	<script type="text/javascript"> 

	jQuery(document).ready(function(){

			jQuery('#slideshow').fadeSlideShow({

				interval: 8000,

        width: 960,

        height: 428,

				PlayPauseElement: false,

				NextElement: false,

				PrevElement: false,

				ListElement: false

		});

		});

	</script> 

	<script type="text/javascript" src="/lineamedica/assets/d8abba8f/jquery.js"></script> 

<script type="text/javascript" src="/lineamedica/js/FadeSlideShow/fadeSlideShow-minified.js"></script> 



Can someone help me?

TIA

I solved it, but it is weird. Before I not include in my post that I had the title tag after the </script> like this.


<?php

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


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

$cs->registerScriptFile(Yii::app()->baseUrl . '/js/FadeSlideShow/fadeSlideShow-minified.js', CClientScript::POS_HEAD);

?>

	<script type="text/javascript">

	jQuery(document).ready(function(){

			jQuery('#slideshow').fadeSlideShow({

				interval: 8000,

        width: 960,

        height: 428,

				PlayPauseElement: false,

				NextElement: false,

				PrevElement: false,

				ListElement: false

		});

		});

	</script>

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



And the result was this:


	<script type="text/javascript"> 

	jQuery(document).ready(function(){

			jQuery('#slideshow').fadeSlideShow({

				interval: 8000,

        width: 960,

        height: 428,

				PlayPauseElement: false,

				NextElement: false,

				PrevElement: false,

				ListElement: false

		});

		});

	</script> 

	<script type="text/javascript" src="/lineamedica/assets/d8abba8f/jquery.js"></script> 

<script type="text/javascript" src="/lineamedica/js/FadeSlideShow/fadeSlideShow-minified.js"></script>

<title>Linea Medica</title> 



Now, I switch <script> with <title> and it is working.


<?php

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


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

$cs->registerScriptFile(Yii::app()->baseUrl . '/js/FadeSlideShow/fadeSlideShow-minified.js', CClientScript::POS_HEAD);

?>

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

	<script type="text/javascript">

	jQuery(document).ready(function(){

			jQuery('#slideshow').fadeSlideShow({

				interval: 8000,

        width: 960,

        height: 428,

				PlayPauseElement: false,

				NextElement: false,

				PrevElement: false,

				ListElement: false

		});

		});

	</script>



and the result now is this one:


	<script type="text/javascript" src="/lineamedica/assets/d8abba8f/jquery.js"></script> 

  <script type="text/javascript" src="/lineamedica/js/FadeSlideShow/fadeSlideShow-minified.js"></script> 

  <title>Linea Medica</title> 

	<script type="text/javascript"> 

	jQuery(document).ready(function(){

			jQuery('#slideshow').fadeSlideShow({

				interval: 8000,

        width: 960,

        height: 428,

				PlayPauseElement: false,

				NextElement: false,

				PrevElement: false,

				ListElement: false

		});

		});

	</script> 



Can someone show me a reason for this is happening?

TIA

It is all about the position you set in the registerScriptFile() method, as the documentation says:




the position of the JavaScript code. Valid values include the following:

CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.

CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.

CClientScript::POS_END : the script is inserted at the end of the body section.



http://www.yiiframework.com/doc/api/1.1/CClientScript#registerScriptFile-detail

Thanks, twisted

Now I understand. :)