diff --git a/docs/guide/view.md b/docs/guide/view.md index 5c4bfe4269..bcb443fd6f 100644 --- a/docs/guide/view.md +++ b/docs/guide/view.md @@ -236,7 +236,8 @@ determines where script should be inserted into the page. Possible values are: - `View::POS_HEAD` for head section. - `View::POS_BEGIN` for right after opening ``. - `View::POS_END` for right before closing ``. -- `View::POS_READY` for executing code on document `ready` event. This one registers jQuery automatically. +- `View::POS_READY` for executing code on document `ready` event. This will register jQuery automatically. +- `View::POS_LOAD` for executing code on document `load` event. This will register jQuery automatically. The last argument is a unique script ID that is used to identify code block and replace existing one with the same ID instead of adding a new one. If you don't provide it, the JS code itself will be used as the ID. diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 8d2baf12e5..de661cf77c 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -108,6 +108,7 @@ Yii Framework 2 Change Log - Enh: Added ability to get incoming headers (dizews) - Enh: Added `beforeRun()` and `afterRun()` to `yii\base\Action` (qiangxue) - Enh: Added support for using timeZone with `yii\base\Formatter` (dizews) +- Enh: Added `yii\web\View::POS_LOAD` (qiangxue) - Chg #1519: `yii\web\User::loginRequired()` now returns the `Response` object instead of exiting the application (qiangxue) - Chg #1586: `QueryBuilder::buildLikeCondition()` will now escape special characters and use percentage characters by default (qiangxue) - Chg #1610: `Html::activeCheckboxList()` and `Html::activeRadioList()` will submit an empty string if no checkbox/radio is selected (qiangxue) diff --git a/framework/web/View.php b/framework/web/View.php index f05791b4b4..df6a0c96c3 100644 --- a/framework/web/View.php +++ b/framework/web/View.php @@ -66,6 +66,11 @@ class View extends \yii\base\View * This means the JavaScript code block will be enclosed within `jQuery(document).ready()`. */ const POS_READY = 4; + /** + * The location of registered JavaScript code block. + * This means the JavaScript code block will be enclosed within `jQuery(window).load()`. + */ + const POS_LOAD = 5; /** * This is internally used as the placeholder for receiving the content registered for the head section. */ @@ -336,6 +341,8 @@ class View extends \yii\base\View * - [[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. * @@ -347,7 +354,7 @@ class View extends \yii\base\View { $key = $key ?: md5($js); $this->js[$position][$key] = $js; - if ($position === self::POS_READY) { + if ($position === self::POS_READY || $position === self::POS_LOAD) { JqueryAsset::register($this); } } @@ -458,6 +465,10 @@ class View extends \yii\base\View $js = "jQuery(document).ready(function(){\n" . implode("\n", $this->js[self::POS_READY]) . "\n});"; $lines[] = Html::script($js, ['type' => 'text/javascript']); } + if (!empty($this->js[self::POS_LOAD])) { + $js = "jQuery(window).load(function(){\n" . implode("\n", $this->js[self::POS_LOAD]) . "\n});"; + $lines[] = Html::script($js, ['type' => 'text/javascript']); + } return empty($lines) ? '' : implode("\n", $lines); } }