The Javascript helper is used to aid in creating well formatted related javascript tags and codeblocks. There are several methods some of which are designed to work with the Prototype Javascript library.
The Javascript Helper is deprecated in 1.3 and will be removed in future versions of CakePHP. See the new JsHelper and HtmlHelper, as well as the migration guide for where JavascriptHelper’s methods have moved to.
codeBlock($script, $options = array('allowCache'=>true,'safe'=>true,'inline'=>true), $safe)
codeBlock returns a formatted script element containing $script. But can
also return null if Javascript helper is set to cache events. See
JavascriptHelper::cacheEvents(). And can write in
$scripts_for_layout
if you set $options[‘inline’] to false.
blockEnd()
Ends a block of cached Javascript. Can return either a end script tag, or empties the buffer, adding the contents to the cachedEvents array. Its return value depends on the cache settings. See JavascriptHelper::cacheEvents()
link($url, $inline = true)
$scripts_for_layout
Creates a javascript link to a single or many javascript files. Can
output inline or in $scripts_for_layout
.
If the filename is prefixed with “/”, the path will be relative to the base path of your application. Otherwise, the path will be relative to your JavaScript path, usually webroot/js.
escapeString($string)
Escape a string to be JavaScript friendly. Allowing it to safely be used in javascript blocks.
The characters that are escaped are:
event($object, $event, $observer, $useCapture)
Attach a javascript event handler specified by $event to an element DOM element specified by $object. Object does not have to be an ID reference it can refer to any valid javascript object or CSS selectors. If a CSS selector is used the event handler is cached and should be retrieved with JavascriptHelper::getCache(). This method requires the Prototype library.
cacheEvents($file, $all)
Allows you to control how the JavaScript Helper caches event code generated by event(). If $all is set to true, all code generated by the helper is cached and can be retrieved with getCache() or written to file or page output with writeCache().
getCache($clear)
Gets (and clears) the current JavaScript event cache
writeEvents($inline)
Returns cached javascript code. If $file was set to true with cacheEvents(), code is cached to a file and a script link to the cached events file is returned. If inline is true, the event code is returned inline. Else it is added to the $scripts_for_layout for the page.
includeScript($script)
Includes the named $script. If $script is left blank the helper will include every script in your app/webroot/js directory. Includes the contents of each file inline. To create a script tag with an src attribute use link().
object($data, $options)
Generates a JavaScript object in JavaScript Object Notation (JSON) from $data array.