yii\web\View::registerJs PHP Method

registerJs() public method

Registers a JS code block.
public registerJs ( string $js, integer $position = self::POS_READY, string $key = null )
$js string the JS code block to be registered
$position integer the position at which the JS script tag should be inserted in a page. The possible values are: - [[POS_HEAD]]: in the head section - [[POS_BEGIN]]: at the beginning of the body section - [[POS_END]]: at the end of the body section - [[POS_LOAD]]: enclosed within jQuery(window).load(). Note that by using this position, the method will automatically register the jQuery js file. - [[POS_READY]]: enclosed within jQuery(document).ready(). This is the default value. Note that by using this position, the method will automatically register the jQuery js file.
$key string the key that identifies the JS code block. If null, it will use $js as the key. If two JS code blocks are registered with the same key, the latter will overwrite the former.
    public function registerJs($js, $position = self::POS_READY, $key = null)
    {
        $key = $key ?: md5($js);
        $this->js[$position][$key] = $js;
        if ($position === self::POS_READY || $position === self::POS_LOAD) {
            JqueryAsset::register($this);
        }
    }

Usage Example

Example #1
3
 /**
  *
  * @param View  $view
  * @param array $data
  */
 public static function bizConfig($view, $config = [], $position = View::POS_BEGIN)
 {
     $default = ['delay' => 1000, 'limit' => 20, 'checkStock' => false, 'debug' => YII_ENV == 'dev', 'pullUrl' => \yii\helpers\Url::to(['/master/sources/pull'])];
     $js = "\n var biz = biz || {};" . "\n biz.config = " . Json::encode(ArrayHelper::merge($default, $config)) . ";\n";
     $view->registerJs($js, $position);
     BizAsset::register($view);
 }
All Usage Examples Of yii\web\View::registerJs