Class CJavaScript
CJavaScript is a helper class containing JavaScript-related handling
functions.
Methods summary
public static
string
|
#
quote( string $js, boolean $forUrl = false )
Quotes a javascript string. After processing, the string can be safely
enclosed within a pair of quotation marks and serve as a javascript string.
Quotes a javascript string. After processing, the string can be safely
enclosed within a pair of quotation marks and serve as a javascript string.
Parameters
- $js
string $js string to be quoted
- $forUrl
boolean $forUrl whether this string is used as a URL
Returns
string the quoted string
|
public static
string
|
#
encode( mixed $value, boolean $safe = false )
Encodes a PHP variable into javascript representation.
Encodes a PHP variable into javascript representation.
Example:
$options=array('key1'=>true,'key2'=>123,'key3'=>'value');
echo CJavaScript::encode($options);
// The following javascript code would be generated:
// {'key1':true,'key2':123,'key3':'value'}
For highly complex data structures use CJavaScript::jsonEncode() and CJavaScript::jsonDecode() to serialize and unserialize.
If you are encoding user input, make sure $safe is set to true.
Parameters
- $value
mixed $value PHP variable to be encoded
- $safe
boolean $safe If true, 'js:' will not be allowed. In case of wrapping code with CJavaScriptExpression JavaScript expression will stay as is no matter what
value this parameter is set to. Default is false. This parameter is available
since 1.1.11.
Returns
string the encoded string
|
public static
string
|
#
jsonEncode( mixed $data )
Returns the JSON representation of the PHP data.
Returns the JSON representation of the PHP data.
Parameters
- $data
mixed $data the data to be encoded
Returns
string the JSON representation of the PHP data.
|
public static
mixed
|
#
jsonDecode( string $data, boolean $useArray = true )
Decodes a JSON string.
Parameters
- $data
string $data the data to be decoded
- $useArray
boolean $useArray whether to use associative array to represent object data
Returns
mixed the decoded PHP data
|