system system.base system.caching system.caching.dependencies system.collections system.console system.db system.db.ar system.db.schema system.db.schema.mssql system.db.schema.mysql system.db.schema.oci system.db.schema.pgsql system.db.schema.sqlite system.i18n system.i18n.gettext system.logging system.utils system.validators system.web system.web.actions system.web.auth system.web.filters system.web.helpers system.web.renderers system.web.services system.web.widgets system.web.widgets.captcha system.web.widgets.pagers

CClientScript

system.web
继承 class CClientScript » CApplicationComponent » CComponent
实现 IApplicationComponent
可用自 1.0
版本 $Id$
CClientScript manages JavaScript and CSS stylesheets for views.

公共属性

隐藏继承的属性

属性类型描述被定义在
behaviors array the behaviors that should be attached to this component. CApplicationComponent
coreScriptUrl string Returns the base URL of all core javascript files. CClientScript
enableJavaScript boolean whether JavaScript should be enabled. CClientScript
isInitialized boolean whether this application component has been initialized (i. CApplicationComponent
scriptMap array the mapping between script file names and the corresponding script URLs. CClientScript

受保护的属性

隐藏继承的属性

属性类型描述被定义在
cssFiles array the registered CSS files (CSS URL=>media type). CClientScript
scriptFiles array the registered JavaScript files (position, key => URL) CClientScript
scripts array the registered JavaScript code blocks (position, key => code) CClientScript

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. CComponent
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
getCoreScriptUrl() Returns the base URL of all core javascript files. CClientScript
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getIsInitialized() CApplicationComponent
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
init() Initializes the application component. 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() Raises an event. CComponent
registerCoreScript() Registers a core javascript library. CClientScript
registerCss() Registers a piece of CSS code. CClientScript
registerCssFile() Registers a CSS file CClientScript
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
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

属性详情

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.

cssFiles 属性 (自版本 v1.0.4 可用)
protected array $cssFiles;

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

enableJavaScript 属性
public boolean $enableJavaScript;

whether JavaScript should be enabled. Defaults to true.

scriptFiles 属性 (自版本 v1.0.4 可用)
protected array $scriptFiles;

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

scriptMap 属性 (自版本 v1.0.3 可用)
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 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 属性 (自版本 v1.0.5 可用)
protected array $scripts;

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

方法详情

getCoreScriptUrl() 方法
public string getCoreScriptUrl()
{return} string the base URL of all core javascript files

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.

isCssFileRegistered() 方法
public boolean isCssFileRegistered(string $url)
$url string URL of the CSS file
{return} boolean whether the CSS file is already registered

Checks whether the CSS file has been registered.

isCssRegistered() 方法
public boolean isCssRegistered(string $id)
$id string ID that uniquely identifies the CSS code
{return} boolean whether the CSS code is already registered

Checks whether the CSS code has been registered.

isScriptFileRegistered() 方法
public boolean isScriptFileRegistered(string $url, integer $position=0)
$url string URL of the javascript file
$position integer the 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} boolean whether the javascript file is already registered

Checks whether the JavaScript file has been registered.

isScriptRegistered() 方法
public boolean isScriptRegistered(string $id, integer $position=4)
$id string ID that uniquely identifies the JavaScript code
$position integer the 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} boolean whether the javascript code is already registered

Checks whether the JavaScript code has been registered.

recordCachingAction() 方法 (自版本 v1.0.5 可用)
protected void recordCachingAction(string $context, string $method, array $params)
$context string a property name of the controller. It refers to an object whose method is being called. If empty it means the controller itself.
$method string the method name
$params array parameters passed to the method

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.

参见

registerCoreScript() 方法
public void registerCoreScript(string $name)
$name string the core javascript library name

Registers a core javascript library.

参见

  • renderCoreScript
registerCss() 方法
public void registerCss(string $id, string $css, string $media='')
$id string ID that uniquely identifies this piece of CSS code
$css string the CSS code
$media string media that the CSS code should be applied to. If empty, it means all media types.

Registers a piece of CSS code.

registerCssFile() 方法
public void registerCssFile(string $url, string $media='')
$url string URL of the CSS file
$media string media that the CSS file should be applied to. If empty, it means all media types.

Registers a CSS file

registerLinkTag() 方法 (自版本 v1.0.1 可用)
public void registerLinkTag(string $relation=NULL, string $type=NULL, string $href=NULL, string $media=NULL, array $options=array ( ))
$relation string rel attribute of the link tag. If null, the attribute will not be generated.
$type string type attribute of the link tag. If null, the attribute will not be generated.
$href string href attribute of the link tag. If null, the attribute will not be generated.
$media string media attribute of the link tag. If null, the attribute will not be generated.
$options array other options in name-value pairs

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

registerMetaTag() 方法 (自版本 v1.0.1 可用)
public void registerMetaTag(string $content, string $name=NULL, string $httpEquiv=NULL, array $options=array ( ))
$content string content attribute of the meta tag
$name string name attribute of the meta tag. If null, the attribute will not be generated
$httpEquiv string http-equiv attribute of the meta tag. If null, the attribute will not be generated
$options array other options in name-value pairs (e.g. 'scheme', 'lang')

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

registerScript() 方法
public void registerScript(string $id, string $script, integer $position=4)
$id string ID that uniquely identifies this piece of JavaScript code
$script string the javascript code
$position integer the 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.

Registers a piece of javascript code.

registerScriptFile() 方法
public void registerScriptFile(string $url, integer $position=0)
$url string URL of the javascript file
$position integer the 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.

Registers a javascript file.

remapScripts() 方法 (自版本 v1.0.3 可用)
protected void remapScripts()

Uses scriptMap to re-map the registered scripts.

render() 方法
public void render(string $output)
$output string the existing output that needs to be inserted with script tags

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)
$output string the output to be inserted with scripts.

Inserts the scripts at the beginning of the body section.

renderBodyEnd() 方法
public void renderBodyEnd(string $output)
$output string the output to be inserted with scripts.

Inserts the scripts at the end of the body section.

renderCoreScripts() 方法 (自版本 v1.0.3 可用)
public void renderCoreScripts()

Renders the specified core javascript library.

renderHead() 方法
public void renderHead(string $output)
$output string the output to be inserted with scripts.

Inserts the scripts in the head section.

reset() 方法
public void reset()

Cleans all registered scripts.

setCoreScriptUrl() 方法
public void setCoreScriptUrl(string $value)
$value string the base URL of all core javascript files.

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.