support scriptFiles[self::POS_LOAD] in CClientScript #198

Closed
qiangxue opened this Issue Feb 15, 2012 · 0 comments

2 participants

@qiangxue
Yii Software LLC member

What steps will reproduce the problem?
register a script file with Yii::app()->clientScript->registerScriptFile('script.js', CClientScript::POS_LOAD);

What is the expected output? What do you see instead?
Expected: loading of the script file on window.load event, see http://code.google.com/speed/page-speed/docs/payload.html#DeferLoadingJS
Output: script files at POS_LOAD are not supported, only scripts are

What version of the product are you using? On what operating system?
yii 1.17

Please provide any additional information below.
i added the functionality like this:

{{{
diff --git a/framework/web/CClientScript.php b/framework/web/CClientScript.php
index 5910cb9..447dcd8 100644
--- a/framework/web/CClientScript.php
+++ b/framework/web/CClientScript.php
@@ -415,6 +415,27 @@ class CClientScript extends CApplicationComponent
foreach($this->scriptFiles[self::POS_END] as $scriptFile)
$html.=CHtml::scriptFile($scriptFile)."\n";
}

  • if(isset($this->scriptFiles[self::POS_LOAD])) {
  • // defer loading of scripts {@link http://code.google.com/speed/pa
  • if($fullPage) {
  • $html.='<script type="text/javascript" charset="utf-8">
  • // Add a script element as a child of the body
  • function downloadJSAtOnload() {';
  • foreach($this->scriptFiles[self::POS_LOAD] as $scriptFile)
  • $html.='var element = document.createElement("scri
  • element.src = "'.$scriptFile.'";
  • document.body.appendChild(element);';
  • }
  • $html.='}'."\n";
  • $html.='// Check for browser support of event handling cap
  • if (window.addEventListener)
  • window.addEventListener("load", downloadJSAtOnload, false)
  • else if (window.attachEvent)
  • window.attachEvent("onload", downloadJSAtOnload);
  • else window.onload = downloadJSAtOnload;
  • </script>'."\n";;
  • }
  • } $scripts=isset($this->scripts[self::POS_END]) ? $this->scripts[self::POS_E if(isset($this->scripts[self::POS_READY])) {

}}}

it works, but i'd like to get rid of the html stuff. if you register a script file with POS_LOAD it adds the following to the body now:

{{{
<script type="text/javascript" charset="utf-8">
// Add a script element as a child of the body
function downloadJSAtOnload() {var element = document.createElement("script");
element.src = "/assets/57b46110/fbf7cc2a6638cd1e22051de87de4d370e8b63679.js";
document.body.appendChild(element);}
// Check for browser support of event handling capability
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;
</script>
}}}

What steps will reproduce the problem?
register a script file with Yii::app()->clientScript->registerScriptFile('/script.js', CClientScript::POS_LOAD);

What is the expected output? What do you see instead?
Expected: loading of the script file on window.load event, see http://code.google.com/speed/page-speed/docs/payload.html#DeferLoadingJS
Output: script files at POS_LOAD are not supported, only scripts are

What version of the product are you using? On what operating system?
yii 1.17

Please provide any additional information below.
i added the functionality like this:

{{{
diff --git a/framework/web/CClientScript.php b/framework/web/CClientScript.php
index 5910cb9..447dcd8 100644
--- a/framework/web/CClientScript.php
+++ b/framework/web/CClientScript.php
@@ -415,6 +415,27 @@ class CClientScript extends CApplicationComponent
foreach($this->scriptFiles[self::POS_END] as $scriptFile)
$html.=CHtml::scriptFile($scriptFile)."\n";
}

  • if(isset($this->scriptFiles[self::POS_LOAD])) {
  • // defer loading of scripts {@link http://code.google.com/speed/pa
  • if($fullPage) {
  • $html.='<script type="text/javascript" charset="utf-8">
  • // Add a script element as a child of the body
  • function downloadJSAtOnload() {';
  • foreach($this->scriptFiles[self::POS_LOAD] as $scriptFile)
  • $html.='var element = document.createElement("scri
  • element.src = "'.$scriptFile.'";
  • document.body.appendChild(element);';
  • }
  • $html.='}'."\n";
  • $html.='// Check for browser support of event handling cap
  • if (window.addEventListener)
  • window.addEventListener("load", downloadJSAtOnload, false)
  • else if (window.attachEvent)
  • window.attachEvent("onload", downloadJSAtOnload);
  • else window.onload = downloadJSAtOnload;
  • </script>'."\n";;
  • }
  • } $scripts=isset($this->scripts[self::POS_END]) ? $this->scripts[self::POS_E if(isset($this->scripts[self::POS_READY])) {

}}}

it works, but i'd like to get rid of the html stuff. Loading time of my site has decreased from 900 to 500 ms with this change.

If you register a script file with POS_LOAD it adds the following to the end of the body now

{{{
<script type="text/javascript" charset="utf-8">
// Add a script element as a child of the body
function downloadJSAtOnload() {var element = document.createElement("script");
element.src = "script.js";
document.body.appendChild(element);}
// Check for browser support of event handling capability
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;
</script>
}}}

Migrated from http://code.google.com/p/yii/issues/detail?id=2279


earlier comments

stoeckjakob said, at 2011-03-31T14:04:34.000Z:

sorry for the doublepost. the second version is the complete one.

qiang.xue said, at 2012-01-01T03:36:54.000Z:

set for 1.1.10 milestone

@samdark samdark closed this Nov 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment