jump to navigation

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’));

Advertisements

Comments»

1. Sam D - February 23, 2006

quick question does your helper extent the javascript helper? or the html helper. Or have you defined your own link helper function as well?

2. rossoft - February 23, 2006

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(…)
{

}
}

3. richardathome - February 24, 2006

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.

4. Sam D - February 25, 2006

I was mostly wondering if $this->link was using the link in cake’s html helper or your own is all.

5. rossoft - February 25, 2006

$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/

6. IT Support Manchester - September 29, 2009

Hi there some great coding tips there thanks very much, will go and put some of this theory to the test.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: