views:

560

answers:

4

Hi not sure if this is possible or not but I want to programaticaly update the <body> tags to change the onload function in my zend framework application.

The App is using layouts so the body tag currently looks like this <body class="trandra">

However in one of my views I have a map from google being loaded and it needs the following in the body tag this particular view <body onload="initialize()" onunload="GUnload()">

As you can understand I don't want this to be hardcoded in my layout as this will cause all matter of nightmares with the different views.

How can this be done programaticaly, if at all it is possible? Im using the headScript functions to add the javascript so is there an equivalant for the body tag?

Thanks in advance...

+2  A: 

Instead of putting your Javascript directly in the code, you could also use an non-obstrusive approch : plugging in the javascript when the page is fully loaded.

Have a look, for instance, at a function called addOnLoadEvent (can be found on many websites ^^ )

If you are using a JS Framework, it certainly has that kind of feature :

If you register the "plugging-in" with headScript, there should be no need to modify the tag directly.

Pascal MARTIN
A: 

I'm no expert on the Zend framework, so I don't know if there is any build in functions for this, but you could do something like this: In layout-file: body_params?>>

And then in your controller, you set or add to the body_params:

$this->view->body_params='onload="initialize()" onunload="GUnload()"';
Torandi
+3  A: 

Approach one - Use a layout variable

One idea would be the following:

<body class="trandra" <?php echo $this->layout()->bodyScripts ?>>

And in your view:

<?php
   $this->layout->bodyScripts = 
       'onload="initialize()" onunload="GUnload()"';

Approach two - Additional JS-file that adds event handlers

Another approach, which is less obtrusive and doesn't affect the HTML whatsoever is to add an additional JS-file in the view that requires the onload- and onunload-handlers. It could look something like this:

<?php
   $this->headScript()->appendScript(
                         '/path/to/javascripts/loadGMaps.js');

In your loadGMaps.js (using prototype)

Event.observe(window, 'load', function onLoadHandler() {
    // Code for initializing Google maps here
});

Event.observe(window, 'unload', function onUnloadHandler() {
    // Code for unloading Google maps here
});
PatrikAkerstrand
A: 

Hi guys,

Developed something like this recently, I've blogged about it here: http://www.evilprofessor.co.uk/311-zend-framework-body-tag-view-helper/

Demo on site and code is available via github.

Cheers, Lloyd.

Lloyd Watkin