Javascript Links February 23, 2006
Posted by rossoft in CakePHP.trackback
When you want to execute a Javascript method to the current page with an anchor link, you have two methods:
Creating a link with <a href=”javascript:new Effect(xxx)”> or with <a href=”#” onClick=”new Efect(xxxx); return false”>
Only the second method worked for me, and is the most standard way. I have created a method in a custom helper for doing it automatically.
/**
* Link to a javascript method
* @param $title Title for the link
* @param url Javascript methods to execute. Separate with a ‘;’
*/
function linkJavascript($title,$url, $htmlAttributes = array(), $confirmMessage = false, $escapeTitle = true,$return=false)
{
$htmlAttributes['onclick']=$url . “; return false;”;
return $this->link($title,’#',$htmlAttributes,$confirmMessage,$escapeTitle,$return);
}
The usage is very simple:
$myhtml->linkJavascript(’Click this’,'new Effect(xxxx’));
quick question does your helper extent the javascript helper? or the html helper. Or have you defined your own link helper function as well?
This is only a method in a helper, nor the complete helper definition. You must create the complete helper or insert that method in your own helper.
For example:
class MyhtmlHelper extends Helper
{
function linkJavascript(…)
{
}
}
Have you seen behaviour? ( http://bennolan.com/behaviour/ )
It allows you to attach javascript events to html elements without dirtying up the markup with onClick handlers.
I was mostly wondering if $this->link was using the link in cake’s html helper or your own is all.
$this->link is using the link of cake’s html helper. I forgot that.
I use the flexifix for easy extending helpers:
http://www.thinkingphp.org/2006/02/01/best-practises-bug-fixing-without-core-hacking/