Allow registering scriptFiles as async #2093

Closed
wants to merge 1 commit into
from

4 participants

@fiesh
fiesh commented Feb 9, 2013

This fixes #1724, it adds the ability to add 'async="async"' to <script> tags in CHtml and also adds an array $asyncScripts to CClientScript such that the scripts listed in it will be tagged async in the resulting HTML file.

@klimov-paul klimov-paul commented on the diff Feb 25, 2013
framework/web/helpers/CHtml.php
* @return string the enclosed JavaScript
*/
- public static function script($text)
+ public static function script($text,$async=false)
klimov-paul
klimov-paul Feb 25, 2013 Member

Instead of adding boolean field, it would be better to pass array "$htmlOptions", which should be used to compose a HTML tag.
See "CHtml::tag()" for ispiration and possible solution.

@klimov-paul klimov-paul commented on the diff Feb 25, 2013
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();
klimov-paul
klimov-paul Feb 25, 2013 Member

It whould be better to modify "CClentScript::registerScriptFile()" adding third parameter for the additional HTML options.

fiesh
fiesh Feb 25, 2013

That would make it really hard to have files register async that are registered by something you don't control. With this current option, you can just have a scriptMap like behavior that is much easier for production use.

klimov-paul
klimov-paul Feb 26, 2013 Member

And what will you do for the "defer" option of script files?
Should we add a new field at "CClientScript" for each possible variations of script file link?

fiesh
fiesh Feb 26, 2013

Nothing because defer does not really seem to be used. Apart from that, the script tag does not really support any useful "possible variations", i.e., attributes: http://www.w3schools.com/tags/tag_script.asp. But whatever, I'll close the pull request.

klimov-paul
klimov-paul Feb 26, 2013 Member

The issue (#1724) is valid.
As for the possible solution, I would like to hear what @yiisoft think about it.

fiesh commented Feb 26, 2013

Just async is not valid XHTML

@fiesh fiesh closed this Feb 26, 2013
@samdark samdark reopened this Feb 26, 2013
Member

Competitor pull request has been opened at #2172

@samdark samdark closed this Mar 9, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment