CClientScript
CClientScript manages JavaScript and CSS stylesheets for views.
Protected Properties
Hide inherited properties
| Property | Type | Description | Defined By |
| css |
array |
the register css code blocks (key => array(CSS code, media type)). |
CClientScript |
| cssFiles |
array |
the registered CSS files (CSS URL=>media type). |
CClientScript |
| linkTags |
array |
the register head link tags. |
CClientScript |
| metaTags |
array |
the register head meta tags. |
CClientScript |
| scriptFiles |
array |
the registered JavaScript files (position, key => URL) |
CClientScript |
| scripts |
array |
the registered JavaScript code blocks (position, key => code) |
CClientScript |
Property Details
public integer $coreScriptPosition;
Where the core scripts will be inserted in the page.
This can be one of the CClientScript::POS_* constants.
Defaults to CClientScript::POS_HEAD.
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.
protected array $css;
the register css code blocks (key => array(CSS code, media type)).
protected array $cssFiles;
the registered CSS files (CSS URL=>media type).
public boolean $enableJavaScript;
whether JavaScript should be enabled. Defaults to true.
protected array $linkTags;
the register head link tags. Each array element represents an option array
that will be passed as the last parameter of CHtml::linkTag.
protected array $metaTags;
the register head meta tags. Each array element represents an option array
that will be passed as the last parameter of CHtml::metaTag.
protected array $scriptFiles;
the registered JavaScript files (position, key => URL)
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.
protected array $scripts;
the registered JavaScript code blocks (position, key => code)
Method Details
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
public void registerCoreScript(string $name)
|
| $name |
string |
the core javascript library name |
Registers a core javascript library.
|
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.
|
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
|
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.
|
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.
|
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.
|
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.
|
protected void remapScripts()
|
Uses scriptMap to re-map the registered scripts.
|
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.
|
public void renderCoreScripts()
|
Renders the specified core javascript library.
|
public void renderHead(string $output)
|
| $output |
string |
the output to be inserted with scripts. |
Inserts the scripts in the head section.
Cleans all registered scripts.
|
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.