1. Make sure in your layout, in the <head> you have this:

   <?php echo $scripts_for_layout; ?>

  Then, in whatever view you need to add the script, just do:

<?php $javascript->link('js_file', false); ?>

(the `false` parameter means it isn't processed directly [hence no
need for `echo()`] but added to the layout's `$scripts_for_layout`
variable).

2. Erm... do people seriously still use that construct? What about all
the cross-browser `addEvent` implementations and `onDomReady` events?
Every javascript framework known to man has them - don't you think
there's a reason?

Still, your decision - perhaps you have design requirements. To answer
your question, the best way I can think of would be :

(in the view)
<?php $this->set('bodyLoadEvent', 'doSomething()'); ?>

(in the layout)
<?php if (empty($bodyLoadEvent)) { ?>
<body>
<?php } else { echo '<body onload="'.$bodyLoadEvent.'">'; } ?>

(Note that one branch uses "normal" html, the other uses `echo()` and
a string - this will help code editors to syntax hilight properly and
generally not get confused.)

Escaping the string to a html-and-javascript--friendly format might be
required though.

Hope this helps
grigri

On Apr 22, 10:15 am, . <[EMAIL PROTECTED]> wrote:
> in cake 1,2 How do you add a javascript function in just one of my views? I
> don't want to put it in the layout because i don't need the JS in all of the
> pages.
>
> There are 2 cases:
> 1. I want to add echo $javascript->link(array(js_file));  to one of my view
> ctp file
> 2. I want to add a <body onload="JS function"> to on of my pages
>
> How would I achieve the above without creating a new layout ctp file?
>
> Thanks
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to