CClientScript

优质
小牛编辑
137浏览
2023-12-01
所有包 | 属性 | 方法
system.web
继承class CClientScript » CApplicationComponent » CComponent
实现IApplicationComponent
源自1.0
版本$Id: CClientScript.php 3559 2012-02-09 21:12:53Z alexander.makarow $
源码framework/web/CClientScript.php
CClientScript manages JavaScript and CSS stylesheets for views.

公共属性

隐藏继承属性

属性类型描述定义在
behaviorsarray这个应用组件附加的行为。 这此行为将在应用组件调用init时附加在应用组件上。 请参照CModel::behaviors如何指定此属性值。CApplicationComponent
corePackagesarraylist of core script packages (name=>package spec).CClientScript
coreScriptPositionintegerWhere the scripts registered using registerCoreScript or registerPackage will be inserted in the page.CClientScript
coreScriptUrlstringReturns the base URL of all core javascript files.CClientScript
enableJavaScriptbooleanwhether JavaScript should be enabled.CClientScript
isInitializedboolean检查应用组件是否已经初始化。CApplicationComponent
packagesarraylist of custom script packages (name=>package spec).CClientScript
scriptMaparraythe mapping between script file names and the corresponding script URLs.CClientScript

受保护属性

隐藏继承属性

属性类型描述定义在
coreScriptsarraythe registered script packages (name => package spec)CClientScript
cssarraythe registered css code blocks (key => array(CSS code, media type)).CClientScript
cssFilesarraythe registered CSS files (CSS URL=>media type).CClientScript
hasScriptsbooleanwhether there are any javascript or css to be rendered.CClientScript
linkTagsarraythe registered head link tags.CClientScript
metaTagsarraythe registered head meta tags.CClientScript
scriptFilesarraythe registered JavaScript files (position, key => URL)CClientScript
scriptsarraythe registered JavaScript code blocks (position, key => code)CClientScript

公共方法

隐藏继承方法

方法描述定义在
__call()如果类中没有调的方法名,则调用这个方法。CComponent
__get()返回一个属性值、一个事件处理程序列表或一个行为名称。CComponent
__isset()检查一个属性是否为null。CComponent
__set()设置一个组件的属性值。CComponent
__unset()设置一个组件的属性为null。CComponent
addPackage()Adds a package to packages list.CClientScript
asa()返回这个名字的行为对象。CComponent
attachBehavior()附加一个行为到组件。CComponent
attachBehaviors()附加一个行为列表到组件。CComponent
attachEventHandler()为事件附加一个事件处理程序。CComponent
canGetProperty()确定属性是否可读。CComponent
canSetProperty()确定属性是否可写。CComponent
detachBehavior()从组件中分离一个行为。CComponent
detachBehaviors()从组件中分离所有行为。CComponent
detachEventHandler()分离一个存在的事件处理程序。CComponent
disableBehavior()禁用一个附加行为。CComponent
disableBehaviors()禁用组件附加的所有行为。CComponent
enableBehavior()启用一个附加行为。CComponent
enableBehaviors()启用组件附加的所有行为。CComponent
evaluateExpression()计算一个PHP表达式,或根据组件上下文执行回调。CComponent
getCoreScriptUrl()Returns the base URL of all core javascript files.CClientScript
getEventHandlers()返回一个事件的附加处理程序列表。CComponent
getIsInitialized()检查应用组件是否已经初始化。CApplicationComponent
getPackageBaseUrl()Returns the base URL for a registered package with the specified name.CClientScript
hasEvent()确定一个事件是否定义。CComponent
hasEventHandler()检查事件是否有附加的处理程序。CComponent
hasProperty()确定属性是否被定义。CComponent
init()初始化应用组件。CApplicationComponent
isCssFileRegistered()Checks whether the CSS file has been registered.CClientScript
isCssRegistered()Checks whether the CSS code has been registered.CClientScript
isScriptFileRegistered()Checks whether the JavaScript file has been registered.CClientScript
isScriptRegistered()Checks whether the JavaScript code has been registered.CClientScript
raiseEvent()发起一个事件。CComponent
registerCoreScript()Registers a script package that is listed in packages.CClientScript
registerCss()Registers a piece of CSS code.CClientScript
registerCssFile()Registers a CSS fileCClientScript
registerLinkTag()Registers a link tag that will be inserted in the head section (right before the title element) of the resulting page.CClientScript
registerMetaTag()Registers a meta tag that will be inserted in the head section (right before the title element) of the resulting page.CClientScript
registerPackage()Registers a script package that is listed in packages.CClientScript
registerScript()Registers a piece of javascript code.CClientScript
registerScriptFile()Registers a javascript file.CClientScript
render()Renders the registered scripts.CClientScript
renderBodyBegin()Inserts the scripts at the beginning of the body section.CClientScript
renderBodyEnd()Inserts the scripts at the end of the body section.CClientScript
renderCoreScripts()Renders the specified core javascript library.CClientScript
renderHead()Inserts the scripts in the head section.CClientScript
reset()Cleans all registered scripts.CClientScript
setCoreScriptUrl()Sets the base URL of all core javascript files.CClientScript

受保护方法

隐藏继承方法

方法描述定义在
recordCachingAction()Records a method call when an output cache is in effect.CClientScript
remapScripts()Uses scriptMap to re-map the registered scripts.CClientScript
unifyScripts()Removes duplicated scripts from scriptFiles.CClientScript

属性详细

corePackages 属性 (可用自 v1.1.7) public array $corePackages;

list of core script packages (name=>package spec). Please refer to packages for details about package spec.

By default, the core script packages are specified in 'framework/web/js/packages.php'. You may configure this property to customize the core script packages.

When calling registerPackage to register a script package, packages will be checked first followed by this property. If a package is found, it will be registered for rendering later on.

coreScriptPosition 属性 (可用自 v1.1.3) public integer $coreScriptPosition;

Where the scripts registered using registerCoreScript or registerPackage will be inserted in the page. This can be one of the CClientScript::POS_* constants. Defaults to CClientScript::POS_HEAD.

coreScriptUrl 属性 public string getCoreScriptUrl()
public void setCoreScriptUrl(string $value)

Returns the base URL of all core javascript files. If the base URL is not explicitly set, this method will publish the whole directory 'framework/web/js/source' and return the corresponding URL.

coreScripts 属性 (可用自 v1.1.7) protected array $coreScripts;

the registered script packages (name => package spec)

css 属性 (可用自 v1.1.3) protected array $css;

the registered css code blocks (key => array(CSS code, media type)).

cssFiles 属性 protected array $cssFiles;

the registered CSS files (CSS URL=>media type).

enableJavaScript 属性 public boolean $enableJavaScript;

whether JavaScript should be enabled. Defaults to true.

hasScripts 属性 (可用自 v1.1.7) protected boolean $hasScripts;

whether there are any javascript or css to be rendered.

linkTags 属性 (可用自 v1.1.3) protected array $linkTags;

the registered head link tags. Each array element represents an option array that will be passed as the last parameter of CHtml::linkTag.

metaTags 属性 (可用自 v1.1.3) protected array $metaTags;

the registered head meta tags. Each array element represents an option array that will be passed as the last parameter of CHtml::metaTag.

packages 属性 (可用自 v1.1.7) public array $packages;

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. By calling registerPackage, one can register a whole package of client scripts together with their dependent packages and render them in the HTML output.

The array structure is as follows:

array('package-name'=>array(  'basePath'=>'alias of the directory containing the script files',  'baseUrl'=>'base URL for the script files',  'js'=>array(list of js files relative to basePath/baseUrl),  'css'=>array(list of css files relative to basePath/baseUrl),  'depends'=>array(list of dependent packages),),......
)


The JS and CSS files listed are relative to 'basePath'. For example, if 'basePath' is 'application.assets', a script named 'comments.js' will refer to the file 'protected/assets/comments.js'.

When a script is being rendered in HTML, it will be prefixed with 'baseUrl'. For example, if 'baseUrl' is '/assets', the 'comments.js' script will be rendered using URL '/assets/comments.js'.

If 'baseUrl' does not start with '/', the relative URL of the application entry script will be inserted at the beginning. For example, if 'baseUrl' is 'assets' and the current application runs with the URL 'http://localhost/demo/index.php', then the 'comments.js' script will be rendered using URL '/demo/assets/comments.js'.

If 'baseUrl' is not set, the script will be published by CAssetManager and the corresponding published URL will be used.

When calling registerPackage to register a script package, this property will be checked first followed by corePackages. If a package is found, it will be registered for rendering later on.

scriptFiles 属性 protected array $scriptFiles;

the registered JavaScript files (position, key => URL)

scriptMap 属性 public array $scriptMap;

the mapping between script file names and the corresponding script URLs. The array keys are script file names (without directory part) and the array values are the corresponding URLs. If an array value is false, the corresponding script file will not be rendered. If an array key is '*.js' or '*.css', the corresponding URL will replace all JavaScript files or CSS files, respectively.

This property is mainly used to optimize the generated HTML pages by merging different scripts files into fewer and optimized script files.

scripts 属性 protected array $scripts;

the registered JavaScript code blocks (position, key => code)

方法详细

addPackage() 方法 (可用自 v1.1.9)
public CClientScript addPackage(string $name, array $definition)
$namestringthe name of the script package.
$definitionarraythe definition array of the script package,
{return}CClientScriptthe CClientScript object itself (to support method chaining, available since version 1.1.10).
源码: framework/web/CClientScript.php#751 (显示) publicfunctionaddPackage($name,$definition)
{
$this->packages[$name]=$definition;
return$this;
}

Adds a package to packages list.

参见

  • CClientScript::packages.
getCoreScriptUrl() 方法
public string getCoreScriptUrl()
{return}stringthe base URL of all core javascript files
源码: framework/web/CClientScript.php#444 (显示) publicfunctiongetCoreScriptUrl()
{
if($this->_baseUrl!==null)
return$this->_baseUrl;
else
return$this->_baseUrl=Yii::app()->getAssetManager()->publish(YII_PATH.'/web/js/source');
}

Returns the base URL of all core javascript files. If the base URL is not explicitly set, this method will publish the whole directory 'framework/web/js/source' and return the corresponding URL.

getPackageBaseUrl() 方法 (可用自 v1.1.8)
public string getPackageBaseUrl(string $name)
$namestringthe package name
{return}stringthe base URL for the named package. False is returned if the package is not registered yet.
源码: framework/web/CClientScript.php#471 (显示) publicfunctiongetPackageBaseUrl($name)
{
if(!isset($this->coreScripts[$name]))
returnfalse;
$package=$this->coreScripts[$name];
if(isset($package['baseUrl']))
{
$baseUrl=$package['baseUrl'];
if($baseUrl===''||$baseUrl[0]!=='/'&&strpos($baseUrl,'://')===false)
$baseUrl=Yii::app()->getRequest()->getBaseUrl().'/'.$baseUrl;
$baseUrl=rtrim($baseUrl,'/');
}
elseif(isset($package['basePath']))
$baseUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias($package['basePath']));
else
$baseUrl=$this->getCoreScriptUrl();

return$this->coreScripts[$name]['baseUrl']=$baseUrl;
}

Returns the base URL for a registered package with the specified name. If needed, this method may publish the assets of the package and returns the published base URL.

参见

  • registerPackage
isCssFileRegistered() 方法
public boolean isCssFileRegistered(string $url)
$urlstringURL of the CSS file
{return}booleanwhether the CSS file is already registered
源码: framework/web/CClientScript.php#676 (显示) publicfunctionisCssFileRegistered($url)
{
returnisset($this->cssFiles[$url]);
}

Checks whether the CSS file has been registered.

isCssRegistered() 方法
public boolean isCssRegistered(string $id)
$idstringID that uniquely identifies the CSS code
{return}booleanwhether the CSS code is already registered
源码: framework/web/CClientScript.php#686 (显示) publicfunctionisCssRegistered($id)
{
returnisset($this->css[$id]);
}

Checks whether the CSS code has been registered.

isScriptFileRegistered() 方法
public boolean isScriptFileRegistered(string $url, integer $position=0)
$urlstringURL of the javascript file
$positionintegerthe position of the JavaScript code. Valid values include the following:
  • CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.
  • CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.
  • CClientScript::POS_END : the script is inserted at the end of the body section.
{return}booleanwhether the javascript file is already registered
源码: framework/web/CClientScript.php#702 (显示) publicfunctionisScriptFileRegistered($url,$position=self::POS_HEAD)
{
returnisset($this->scriptFiles[$position][$url]);
}

Checks whether the JavaScript file has been registered.

isScriptRegistered() 方法
public boolean isScriptRegistered(string $id, integer $position=4)
$idstringID that uniquely identifies the JavaScript code
$positionintegerthe position of the JavaScript code. Valid values include the following:
  • CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.
  • CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.
  • CClientScript::POS_END : the script is inserted at the end of the body section.
  • CClientScript::POS_LOAD : the script is inserted in the window.onload() function.
  • CClientScript::POS_READY : the script is inserted in the jQuery's ready function.
{return}booleanwhether the javascript code is already registered
源码: framework/web/CClientScript.php#720 (显示) publicfunctionisScriptRegistered($id,$position=self::POS_READY)
{
returnisset($this->scripts[$position][$id]);
}

Checks whether the JavaScript code has been registered.

recordCachingAction() 方法
protected void recordCachingAction(string $context, string $method, array $params)
$contextstringa property name of the controller. It refers to an object whose method is being called. If empty it means the controller itself.
$methodstringthe method name
$paramsarrayparameters passed to the method
源码: framework/web/CClientScript.php#735 (显示) protectedfunctionrecordCachingAction($context,$method,$params)
{
if(($controller=Yii::app()->getController())!==null)
$controller->recordCachingAction($context,$method,$params);
}

Records a method call when an output cache is in effect. This is a shortcut to Yii::app()->controller->recordCachingAction. In case when controller is absent, nothing is recorded.

参见

  • COutputCache
registerCoreScript() 方法
public CClientScript registerCoreScript(string $name)
$namestringthe name of the script package.
{return}CClientScriptthe CClientScript object itself (to support method chaining, available since version 1.1.5).
源码: framework/web/CClientScript.php#510 (显示) publicfunctionregisterCoreScript($name)
{
if(isset($this->coreScripts[$name]))
return$this;
if(isset($this->packages[$name]))
$package=$this->packages[$name];
else
{
if($this->corePackages===null)
$this->corePackages=require(YII_PATH.'/web/js/packages.php');
if(isset($this->corePackages[$name]))
$package=$this->corePackages[$name];
}
if(isset($package))
{
if(!empty($package['depends']))
{
foreach($package['depends']as$p)
$this->registerCoreScript($p);
}
$this->coreScripts[$name]=$package;
$this->hasScripts=true;
$params=func_get_args();
$this->recordCachingAction('clientScript','registerCoreScript',$params);
}
return$this;
}

Registers a script package that is listed in packages.

参见

  • renderCoreScript
registerCss() 方法
public CClientScript registerCss(string $id, string $css, string $media='')
$idstringID that uniquely identifies this piece of CSS code
$cssstringthe CSS code
$mediastringmedia that the CSS code should be applied to. If empty, it means all media types.
{return}CClientScriptthe CClientScript object itself (to support method chaining, available since version 1.1.5).
源码: framework/web/CClientScript.php#560 (显示) publicfunctionregisterCss($id,$css,$media='')
{
$this->hasScripts=true;
$this->css[$id]=array($css,$media);
$params=func_get_args();
$this->recordCachingAction('clientScript','registerCss',$params);
return$this;
}

Registers a piece of CSS code.

registerCssFile() 方法
public CClientScript registerCssFile(string $url, string $media='')
$urlstringURL of the CSS file
$mediastringmedia that the CSS file should be applied to. If empty, it means all media types.
{return}CClientScriptthe CClientScript object itself (to support method chaining, available since version 1.1.5).
源码: framework/web/CClientScript.php#544 (显示) publicfunctionregisterCssFile($url,$media='')
{
$this->hasScripts=true;
$this->cssFiles[$url]=$media;
$params=func_get_args();
$this->recordCachingAction('clientScript','registerCssFile',$params);
return$this;
}

Registers a CSS file

registerLinkTag() 方法
public CClientScript registerLinkTag(string $relation=NULL, string $type=NULL, string $href=NULL, string $media=NULL, array $options=array ( ))
$relationstringrel attribute of the link tag. If null, the attribute will not be generated.
$typestringtype attribute of the link tag. If null, the attribute will not be generated.
$hrefstringhref attribute of the link tag. If null, the attribute will not be generated.
$mediastringmedia attribute of the link tag. If null, the attribute will not be generated.
$optionsarrayother options in name-value pairs
{return}CClientScriptthe CClientScript object itself (to support method chaining, available since version 1.1.5).
源码: framework/web/CClientScript.php#654 (显示) publicfunctionregisterLinkTag($relation=null,$type=null,$href=null,$media=null,$options=array())
{
$this->hasScripts=true;
if($relation!==null)
$options['rel']=$relation;
if($type!==null)
$options['type']=$type;
if($href!==null)
$options['href']=$href;
if($media!==null)
$options['media']=$media;
$this->linkTags[serialize($options)]=$options;
$params=func_get_args();
$this->recordCachingAction('clientScript','registerLinkTag',$params);
return$this;
}

Registers a link tag that will be inserted in the head section (right before the title element) of the resulting page.

registerMetaTag() 方法
public CClientScript registerMetaTag(string $content, string $name=NULL, string $httpEquiv=NULL, array $options=array ( ))
$contentstringcontent attribute of the meta tag
$namestringname attribute of the meta tag. If null, the attribute will not be generated
$httpEquivstringhttp-equiv attribute of the meta tag. If null, the attribute will not be generated
$optionsarrayother options in name-value pairs (e.g. 'scheme', 'lang')
{return}CClientScriptthe CClientScript object itself (to support method chaining, available since version 1.1.5).
源码: framework/web/CClientScript.php#631 (显示) publicfunctionregisterMetaTag($content,$name=null,$httpEquiv=null,$options=array())
{
$this->hasScripts=true;
if($name!==null)
$options['name']=$name;
if($httpEquiv!==null)
$options['http-equiv']=$httpEquiv;
$options['content']=$content;
$this->metaTags[serialize($options)]=$options;
$params=func_get_args();
$this->recordCachingAction('clientScript','registerMetaTag',$params);
return$this;
}

Registers a meta tag that will be inserted in the head section (right before the title element) of the resulting page.

Note: Meta tags with same attributes will be rendered more then once if called with different values.

Example:

 $cs->registerMetaTag('example', 'description', null, array('lang' => 'en')); $cs->registerMetaTag('beispiel', 'description', null, array('lang' => 'de'));

registerPackage() 方法 (可用自 v1.1.7)
public CClientScript registerPackage(string $name)
$namestringthe name of the script package.
{return}CClientScriptthe CClientScript object itself (to support method chaining, available since version 1.1.5).
源码: framework/web/CClientScript.php#499 (显示) publicfunctionregisterPackage($name)
{
return$this->registerCoreScript($name);
}

Registers a script package that is listed in packages. This method is the same as registerCoreScript.

参见

  • renderCoreScript
registerScript() 方法
public CClientScript registerScript(string $id, string $script, integer $position=4)
$idstringID that uniquely identifies this piece of JavaScript code
$scriptstringthe javascript code
$positionintegerthe position of the JavaScript code. Valid values include the following:
  • CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.
  • CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.
  • CClientScript::POS_END : the script is inserted at the end of the body section.
  • CClientScript::POS_LOAD : the script is inserted in the window.onload() function.
  • CClientScript::POS_READY : the script is inserted in the jQuery's ready function.
{return}CClientScriptthe CClientScript object itself (to support method chaining, available since version 1.1.5).
源码: framework/web/CClientScript.php#603 (显示) publicfunctionregisterScript($id,$script,$position=self::POS_READY)
{
$this->hasScripts=true;
$this->scripts[$position][$id]=$script;
if($position===self::POS_READY||$position===self::POS_LOAD)
$this->registerCoreScript('jquery');
$params=func_get_args();
$this->recordCachingAction('clientScript','registerScript',$params);
return$this;
}

Registers a piece of javascript code.

registerScriptFile() 方法
public CClientScript registerScriptFile(string $url, integer $position=0)
$urlstringURL of the javascript file
$positionintegerthe position of the JavaScript code. Valid values include the following:
  • CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.
  • CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.
  • CClientScript::POS_END : the script is inserted at the end of the body section.
{return}CClientScriptthe CClientScript object itself (to support method chaining, available since version 1.1.5).
源码: framework/web/CClientScript.php#580 (显示) publicfunctionregisterScriptFile($url,$position=self::POS_HEAD)
{
$this->hasScripts=true;
$this->scriptFiles[$position][$url]=$url;
$params=func_get_args();
$this->recordCachingAction('clientScript','registerScriptFile',$params);
return$this;
}

Registers a javascript file.

remapScripts() 方法
protected void remapScripts()
源码: framework/web/CClientScript.php#246 (显示) protectedfunctionremapScripts()
{
$cssFiles=array();
foreach($this->cssFilesas$url=>$media)
{
$name=basename($url);
if(isset($this->scriptMap[$name]))
{
if($this->scriptMap[$name]!==false)
$cssFiles[$this->scriptMap[$name]]=$media;
}
elseif(isset($this->scriptMap['*.css']))
{
if($this->scriptMap['*.css']!==false)
$cssFiles[$this->scriptMap['*.css']]=$media;
}
else
$cssFiles[$url]=$media;
}
$this->cssFiles=$cssFiles;

$jsFiles=array();
foreach($this->scriptFilesas$position=>$scripts)
{
$jsFiles[$position]=array();
foreach($scriptsas$key=>$script)
{
$name=basename($script);
if(isset($this->scriptMap[$name]))
{
if($this->scriptMap[$name]!==false)
$jsFiles[$position][$this->scriptMap[$name]]=$this->scriptMap[$name];
}
elseif(isset($this->scriptMap['*.js']))
{
if($this->scriptMap['*.js']!==false)
$jsFiles[$position][$this->scriptMap['*.js']]=$this->scriptMap['*.js'];
}
else
$jsFiles[$position][$key]=$script;
}
}
$this->scriptFiles=$jsFiles;
}

Uses scriptMap to re-map the registered scripts.

render() 方法
public void render(string &$output)
$outputstringthe existing output that needs to be inserted with script tags
源码: framework/web/CClientScript.php#190 (显示) publicfunctionrender(&$output)
{
if(!$this->hasScripts)
return;

$this->renderCoreScripts();

if(!empty($this->scriptMap))
$this->remapScripts();

$this->unifyScripts();

$this->renderHead($output);
if($this->enableJavaScript)
{
$this->renderBodyBegin($output);
$this->renderBodyEnd($output);
}
}

Renders the registered scripts. This method is called in CController::render when it finishes rendering content. CClientScript thus gets a chance to insert script tags at head and body sections in the HTML output.

renderBodyBegin() 方法
public void renderBodyBegin(string &$output)
$outputstringthe output to be inserted with scripts.
源码: framework/web/CClientScript.php#374 (显示) publicfunctionrenderBodyBegin(&$output)
{
$html='';
if(isset($this->scriptFiles[self::POS_BEGIN]))
{
foreach($this->scriptFiles[self::POS_BEGIN]as$scriptFile)
$html.=CHtml::scriptFile($scriptFile)."n";
}
if(isset($this->scripts[self::POS_BEGIN]))
$html.=CHtml::script(implode("n",$this->scripts[self::POS_BEGIN]))."n";

if($html!=='')
{
$count=0;
$output=preg_replace('/(<bodyb[^>]*>)/is','$1<###begin###>',$output,1,$count);
if($count)
$output=str_replace('<###begin###>',$html,$output);
else
$output=$html.$output;
}
}

Inserts the scripts at the beginning of the body section.

renderBodyEnd() 方法
public void renderBodyEnd(string &$output)
$outputstringthe output to be inserted with scripts.
源码: framework/web/CClientScript.php#400 (显示) publicfunctionrenderBodyEnd(&$output)
{
if(!isset($this->scriptFiles[self::POS_END])&&!isset($this->scripts[self::POS_END])
&&!isset($this->scripts[self::POS_READY])&&!isset($this->scripts[self::POS_LOAD]))
return;

$fullPage=0;
$output=preg_replace('/(<\/bodys*>)/is','<###end###>$1',$output,1,$fullPage);
$html='';
if(isset($this->scriptFiles[self::POS_END]))
{
foreach($this->scriptFiles[self::POS_END]as$scriptFile)
$html.=CHtml::scriptFile($scriptFile)."n";
}
$scripts=isset($this->scripts[self::POS_END])?$this->scripts[self::POS_END]:array();
if(isset($this->scripts[self::POS_READY]))
{
if($fullPage)
$scripts[]="jQuery(function($){n".implode("n",$this->scripts[self::POS_READY])."n});";
else
$scripts[]=implode("n",$this->scripts[self::POS_READY]);
}
if(isset($this->scripts[self::POS_LOAD]))
{
if($fullPage)
$scripts[]="jQuery(window).load(function(){n".implode("n",$this->scripts[self::POS_LOAD])."n});";
else
$scripts[]=implode("n",$this->scripts[self::POS_LOAD]);
}
if(!empty($scripts))
$html.=CHtml::script(implode("n",$scripts))."n";

if($fullPage)
$output=str_replace('<###end###>',$html,$output);
else
$output=$output.$html;
}

Inserts the scripts at the end of the body section.

renderCoreScripts() 方法
public void renderCoreScripts()
源码: framework/web/CClientScript.php#294 (显示) publicfunctionrenderCoreScripts()
{
if($this->coreScripts===null)
return;
$cssFiles=array();
$jsFiles=array();
foreach($this->coreScriptsas$name=>$package)
{
$baseUrl=$this->getPackageBaseUrl($name);
if(!empty($package['js']))
{
foreach($package['js']as$js)
$jsFiles[$baseUrl.'/'.$js]=$baseUrl.'/'.$js;
}
if(!empty($package['css']))
{
foreach($package['css']as$css)
$cssFiles[$baseUrl.'/'.$css]='';
}
}
//mergeinplace
if($cssFiles!==array())
{
foreach($this->cssFilesas$cssFile=>$media)
$cssFiles[$cssFile]=$media;
$this->cssFiles=$cssFiles;
}
if($jsFiles!==array())
{
if(isset($this->scriptFiles[$this->coreScriptPosition]))
{
foreach($this->scriptFiles[$this->coreScriptPosition]as$url)
$jsFiles[$url]=$url;
}
$this->scriptFiles[$this->coreScriptPosition]=$jsFiles;
}
}

Renders the specified core javascript library.

renderHead() 方法
public void renderHead(string &$output)
$outputstringthe output to be inserted with scripts.
源码: framework/web/CClientScript.php#336 (显示) publicfunctionrenderHead(&$output)
{
$html='';
foreach($this->metaTagsas$meta)
$html.=CHtml::metaTag($meta['content'],null,null,$meta)."n";
foreach($this->linkTagsas$link)
$html.=CHtml::linkTag(null,null,null,null,$link)."n";
foreach($this->cssFilesas$url=>$media)
$html.=CHtml::cssFile($url,$media)."n";
foreach($this->cssas$css)
$html.=CHtml::css($css[0],$css[1])."n";
if($this->enableJavaScript)
{
if(isset($this->scriptFiles[self::POS_HEAD]))
{
foreach($this->scriptFiles[self::POS_HEAD]as$scriptFile)
$html.=CHtml::scriptFile($scriptFile)."n";
}

if(isset($this->scripts[self::POS_HEAD]))
$html.=CHtml::script(implode("n",$this->scripts[self::POS_HEAD]))."n";
}

if($html!=='')
{
$count=0;
$output=preg_replace('/(<titleb[^>]*>|<\/heads*>)/is','<###head###>$1',$output,1,$count);
if($count)
$output=str_replace('<###head###>',$html,$output);
else
$output=$html.$output;
}
}

Inserts the scripts in the head section.

reset() 方法
public void reset()
源码: framework/web/CClientScript.php#169 (显示) publicfunctionreset()
{
$this->hasScripts=false;
$this->coreScripts=array();
$this->cssFiles=array();
$this->css=array();
$this->scriptFiles=array();
$this->scripts=array();
$this->metaTags=array();
$this->linkTags=array();

$this->recordCachingAction('clientScript','reset',array());
}

Cleans all registered scripts.

setCoreScriptUrl() 方法
public void setCoreScriptUrl(string $value)
$valuestringthe base URL of all core javascript files.
源码: framework/web/CClientScript.php#458 (显示) publicfunctionsetCoreScriptUrl($value)
{
$this->_baseUrl=$value;
}

Sets the base URL of all core javascript files. This setter is provided in case when core javascript files are manually published to a pre-specified location. This may save asset publishing time for large-scale applications.

unifyScripts() 方法 (可用自 v1.1.5)
protected void unifyScripts()
源码: framework/web/CClientScript.php#214 (显示) protectedfunctionunifyScripts()
{
if(!$this->enableJavaScript)
return;
$map=array();
if(isset($this->scriptFiles[self::POS_HEAD]))
$map=$this->scriptFiles[self::POS_HEAD];

if(isset($this->scriptFiles[self::POS_BEGIN]))
{
foreach($this->scriptFiles[self::POS_BEGIN]as$key=>$scriptFile)
{
if(isset($map[$scriptFile]))
unset($this->scriptFiles[self::POS_BEGIN][$key]);
else
$map[$scriptFile]=true;
}
}

if(isset($this->scriptFiles[self::POS_END]))
{
foreach($this->scriptFiles[self::POS_END]as$key=>$scriptFile)
{
if(isset($map[$scriptFile]))
unset($this->scriptFiles[self::POS_END][$key]);
}
}
}

Removes duplicated scripts from scriptFiles.