开发者

How can I by using the FormHelper ,or some other method, render an image link that runs javascript when clicked?

开发者 https://www.devze.com 2022-12-29 16:32 出处:网络
Background: I want to render this: <a href=\"javascript:myfunction(\'Somevar\')\"><img src=\"some.png\" width=\"20\" height=\"20\" border=\"0\" /></a&开发者_开发知识库gt;

Background:

I want to render this:

<a href="javascript:myfunction('Somevar')"><img src="some.png" width="20" height="20" border="0" /></a&开发者_开发知识库gt;  

in a cakephp app. Note: this link is not associated with a model at all, it's just part of a form going off-site.

I'm unsure of how to proceed: I've seen this on the cakephp site, but with all that goes on in cake, I'm not sure about the whole javascript calling business. What's the best way to create such a thing?

EDIT: Cake 1.2 is what I am working with.


It's no problem at all to pass a Javascript function as the second parameter to HtmlHelper::link. This should do the trick:

echo $html->link(
    $html->image('some.png'),
    "javascript:myfunction('Somevar')",
    array('escape'=>false)
);

This invokes HtmlHelper::link, passing an image (generated by HtmlHelper::image) as the HTML around which to wrap the <a />, the Javascript function as the link's action, and informing Cake not to escape the HTML you passed.

Update:
Here's the snippet for Cake 1.2:

echo $html->link(
    $html->image('some.png'),
    "javascript:myfunction('Somevar')",
    null, null, false
);

The final false parameter of HtmlHelper::link instructs Cake not to escape the HTML.

0

精彩评论

暂无评论...
验证码 换一张
取 消