0

I am using

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

for register jquery in Yii 1 top of script but in page added last of scripts? why?

1
  • 1
    "top of script" - which script? The one called? The view? Commented May 22, 2015 at 9:32

1 Answer 1

1

You can use coreScriptPosition for this.

Directly from the docs:

Where the scripts registered using registerCoreScript or registerPackage will be inserted in the page. This can be one of the CClientScript::POS_* constants. Defaults to CClientScript::POS_HEAD.

Use like this:

$cs = Yii::app()->clientScript;
$cs->coreScriptPosition = CClientScript::POS_HEAD;
$cs->registerCoreScript('jquery');

Another useful link to read on this. (Read the comments too)

--Edit--

You can tell Yii where to put the script in the method:

Yii::app()->clientScript->registerScript(string $id, string $script, integer $position=NULL, array $htmlOptions=array ( ));

The $position variable will be the position of the script on your page.

And can be any of these:

  • 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.
  • CClientScript::POS_LOAD : the script is inserted in the window.onload() function.
  • CClientScript::POS_READY : the script is inserted in the jQuery's ready function.
Sign up to request clarification or add additional context in comments.

2 Comments

include(ClientScript.php): failed to open stream: No such file or directory
@HoseinHamedan, should be CClientScript, my bad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.