Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8306037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:14:58+00:00 2026-06-08T18:14:58+00:00

I created a widget that registers its own script like below class MyWidget extends

  • 0

I created a widget that registers its own script like below

class MyWidget extends CWidget {
    public function run(){
        Yii::app()->clientScript->registerScript(__CLASS__, <<<JAVASCRIPT
var a = "Hello World!";
JAVASCRIPT
        , CClientScript::POS_END);
    }
}

And in the layout, I call the widget like this

<?php $this->widget('MyWidget');?>
<?php echo $content;?>

But in a view file, I need the variable declared by that widget.

<?php 
Yii::app()->clientScript->registerScript('script', <<<JAVASCRIPT
    alert(a);
JAVASCRIPT
    , CClientScript::POS_END);
?>

Note that in both registerScript method I use POS_END as the script position since I intend to put all of the scripts (including the CoreScript e.g. jQuery, jQueryUI etc) after the <body> tag.

The problem is that the rendered script will show the one from the view file and after that the one from the widget.

alert(a);
var a = "Hello World!";

As we can see, the above code won’t work so I need to put the the second line above the first line.

Any idea on how to force the order? I’m okay with extending the CClientScript (and creating a new registerScript method) as long as all of the scripts will be rendered in the end position and I don’t have to pull those inline Javascript codes above to a new package or file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:14:59+00:00Added an answer on June 8, 2026 at 6:14 pm

    So I finally find a hack to do this. I extend a new ClientScript class and modify the registerScript method so it will accept another param $level.

    public function registerScript($id, $script, $position = self::POS_END, $level = 1);
    

    Think about $level just like z-index in CSS, except the greater the number of $level is, the lower the position of the script will be.

    For example

    Yii::app()->clientScript->registerScript('script1', '/** SCRIPT #1 **/', CClientScript::POS_END, 1);
    Yii::app()->clientScript->registerScript('script2', '/** SCRIPT #2 **/', CClientScript::POS_END, 2);
    Yii::app()->clientScript->registerScript('script3', '/** SCRIPT #3 **/', CClientScript::POS_END, 1);
    

    Even though script3 is declared after script2, in the rendered script, it will show above script2 since the $level value of script2 is greater than script3‘s.

    Here’s my solution’s code. It’s working like what I want although I’m not sure if the arranging method is optimized enough.

    /**
     * ClientScript manages Javascript and CSS.
     */
    class ClientScript extends CClientScript {
        public $scriptLevels = array();
    
        /**
         * Registers a piece of javascript code.
         * @param string $id ID that uniquely identifies this piece of JavaScript code
         * @param string $script the javascript code
         * @param integer $position the position of the JavaScript code.
         * @param integer $level the rendering priority of the JavaScript code in a position.
         * @return CClientScript the CClientScript object itself (to support method chaining, available since version 1.1.5).
         */
        public function registerScript($id, $script, $position = self::POS_END, $level = 1) {
            $this->scriptLevels[$id] = $level;
            return parent::registerScript($id, $script, $position);
        }
    
        /**
         * Renders the registered scripts.
         * Overriding from CClientScript.
         * @param string $output the existing output that needs to be inserted with script tags
         */
        public function render(&$output) {
            if (!$this->hasScripts)
                return;
    
            $this->renderCoreScripts();
    
            if (!empty($this->scriptMap))
                $this->remapScripts();
    
            $this->unifyScripts();
    
            //===================================
            //Arranging the priority
            $this->rearrangeLevels();
            //===================================
    
            $this->renderHead($output);
            if ($this->enableJavaScript) {
                $this->renderBodyBegin($output);
                $this->renderBodyEnd($output);
            }
        }
    
    
        /**
         * Rearrange the script levels.
         */
        public function rearrangeLevels() {
            $scriptLevels = $this->scriptLevels;
            foreach ($this->scripts as $position => &$scripts) {
                $newscripts = array();
                $tempscript = array();
                foreach ($scripts as $id => $script) {
                    $level = isset($scriptLevels[$id]) ? $scriptLevels[$id] : 1;
                    $tempscript[$level][$id] = $script;
                }
                foreach ($tempscript as $s) {
                    foreach ($s as $id => $script) {
                        $newscripts[$id] = $script;
                    }
                }
                $scripts = $newscripts;
            }
        }
    }
    

    So I just need to put the class as the clientScript component in the config

    'components' => array(
      'clientScript' => array(
          'class' => 'ClientScript'
      )
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was using widget and found that it has hook widget_init. Like below and
I've created a Vista sidebar widget that uses jQuery to fetch XML and display
I would like to create a widget so that my visitors can display it
Hey all, I've created a widget that will essentially unlock a music track, providing
I've created a nice app widget that I could not find on the market,
I created an Android app widget that displays some text. By pressing on the
I've created an widget that will display if you've not outstanding on a long
I created a widget that uses a service with wakelock to update and parse
I created a widget/control that I can reuse which I created by extending RelativeLayout
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.RelativeLayout; public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.