Skip to content
Browse files

Allow registering scripts as async, [closes #1724]

  • Loading branch information...
1 parent a4af88f commit 62e8fd76acd1e933129250c1b1027bfbaadd1c96 fiesh committed Feb 9, 2013
Showing with 20 additions and 8 deletions.
  1. +14 −4 framework/web/CClientScript.php
  2. +6 −4 framework/web/helpers/CHtml.php
View
18 framework/web/CClientScript.php
@@ -56,6 +56,16 @@ class CClientScript extends CApplicationComponent
*/
public $scriptMap=array();
/**
+ * @var array the list of scripts that should always be included asynchronously.
+ * This allows specifying scripts that will be included with 'async="async"' set even if the call to {@link registerClientScript} does not specify it.
+ * This is particularly useful for scripts that appear in {@link $scriptMap}.
+ *
+ * If a script is registered as asynchronous, its name is appended to this array.
+ *
+ * @since 1.1.14
+ */
+ public $asyncScripts=array();
+ /**
* @var array list of custom script packages (name=>package spec).
* This property keeps a list of named script packages, each of which can contain
* a set of CSS and/or JavaScript script files, and their dependent package names.
@@ -362,7 +372,7 @@ public function renderHead(&$output)
if(isset($this->scriptFiles[self::POS_HEAD]))
{
foreach($this->scriptFiles[self::POS_HEAD] as $scriptFile)
- $html.=CHtml::scriptFile($scriptFile)."\n";
+ $html.=CHtml::scriptFile($scriptFile,(in_array($scriptFile,$this->asyncScripts)?true:false))."\n";
}
if(isset($this->scripts[self::POS_HEAD]))
@@ -390,7 +400,7 @@ public function renderBodyBegin(&$output)
if(isset($this->scriptFiles[self::POS_BEGIN]))
{
foreach($this->scriptFiles[self::POS_BEGIN] as $scriptFile)
- $html.=CHtml::scriptFile($scriptFile)."\n";
+ $html.=CHtml::scriptFile($scriptFile,(in_array($scriptFile,$this->asyncScripts)?true:false))."\n";
}
if(isset($this->scripts[self::POS_BEGIN]))
$html.=CHtml::script(implode("\n",$this->scripts[self::POS_BEGIN]))."\n";
@@ -422,7 +432,7 @@ public function renderBodyEnd(&$output)
if(isset($this->scriptFiles[self::POS_END]))
{
foreach($this->scriptFiles[self::POS_END] as $scriptFile)
- $html.=CHtml::scriptFile($scriptFile)."\n";
+ $html.=CHtml::scriptFile($scriptFile,(in_array($scriptFile,$this->asyncScripts)?true:false))."\n";
}
$scripts=isset($this->scripts[self::POS_END]) ? $this->scripts[self::POS_END] : array();
if(isset($this->scripts[self::POS_READY]))
@@ -771,4 +781,4 @@ public function addPackage($name,$definition)
$this->packages[$name]=$definition;
return $this;
}
-}
+}
View
10 framework/web/helpers/CHtml.php
@@ -276,21 +276,23 @@ public static function cssFile($url,$media='')
/**
* Encloses the given JavaScript within a script tag.
* @param string $text the JavaScript to be enclosed
+ * @param boolean $async whether the script should be executed asynchronously, defaults to false. (@since 1.1.14)
* @return string the enclosed JavaScript
*/
- public static function script($text)
+ public static function script($text,$async=false)
{
- return "<script type=\"text/javascript\">\n/*<![CDATA[*/\n{$text}\n/*]]>*/\n</script>";
+ return '<script type="text/javascript"'.($async?' async="async"':'').">\n/*<![CDATA[*/\n{$text}\n/*]]>*/\n</script>";
}
/**
* Includes a JavaScript file.
* @param string $url URL for the JavaScript file
+ * @param boolean $async whether the script should be included asynchronously, defaults to false. (@since 1.1.14)
* @return string the JavaScript file tag
*/
- public static function scriptFile($url)
+ public static function scriptFile($url,$async=false)
{
- return '<script type="text/javascript" src="'.self::encode($url).'"></script>';
+ return '<script type="text/javascript"'.($async?' async="async"':'').' src="'.self::encode($url).'"></script>';
}
/**

0 comments on commit 62e8fd7

Please sign in to comment.
Something went wrong with that request. Please try again.