CJuiWidget::registerScriptFile

Is there any reason why the fourth position parameter on registerScriptFile method is default to CClientScript::POS_END.

By default, the position parameter in registerScriptFile method in CClientScript is always self::POS_HEAD.

I understand putting the all script file at the end, help with the page rendering and it is a common practise on in Javascript world. But it will cause problem with ajax request in Yii.

Currently, to return a page from ajax request. We have to use renderPartial and then do a Yii::app()->clientScript->renderBodyEnd() to render javascript on that page without including the core file again.

For example,




  protected function beforeAction(CAction $action) {

    if (Yii::app()->request->getIsAjaxRequest()) {

      $this->layout = false;

      Yii::app()->clientScript->enableJavaScript = false;

    }

    return true;

  }


  protected function afterAction(CAction $action) {

    if (Yii::app()->request->getIsAjaxRequest()) {

      $output = '';

      Yii::app()->clientScript->renderBodyEnd($output);

      echo $output;

    }

    return true;

  }



The snippet will return the correct view content and script content without including the layout and jquery core script when you do an ajax request. But it will break once you start using any of CJui* widget because the registerScriptFile in CJuiWidget load script file onto the end. When


Yii::app()->clientScript->renderBodyEnd($output);

get executed, those javascript get included again because registerScriptFile include them in the end.

Is there a better way? I like to be able to handle both ajax and non-ajax transparently.

No worry, I resolve the issue.

by add


Yii::app()->clientScript->scriptMap['*.js'] = false;

in beforeAction. So no javascript file get included in ajax request.