Question

I have an app where I need to call some JS in the onLoad event of the BODY tag of two forms. However, I can't find how to modify the tag for just them. Does anyone know?

Frank

Was it helpful?

Solution

inkedmn certainly has provided the right answer for this case, but in general, you can "hand information up" like this:

(in views/controller/view.ctp)

$this->set('bodyAttr', 'onload="something"');

(in views/layouts/default.ctp)

<?php
    if (isset($bodyAttr)) {
        $bodyAttr = " $bodyAttr";
    } else {
        $bodyAttr = null;
    }
?>
<body<?php echo $bodyAttr; ?>>

I often use it like this to add extra classes to a "top level element":

<?php
    if (!isset($docClass)) {
        $docClass = null;
    }
?>
<div id="doc" class="<?php echo $docClass; ?>">

OTHER TIPS

You don't need to modify the body tag to have Javascript execute when the page loads. You could just include something like this in your layout where appropriate:

(jQuery)

$("body").load(
    function(){
        // do stuff when the body element is loaded.
    }
);

Or, if you want to have the code execute when the document.ready event fires:

$(function(){
        // do stuff when the document is ready
    }
);

Or, if you don't want to use jQuery, you could do something like this:

function doStuff(){
    // whatever you want to happen when the load completes
}

window.onload = dostuff;

Good luck - and please clarify your question if this answer isn't satisfactory.

I do the following:

We apply $ bodyLoad in my body

<body <? = (isset ($ bodyLoad)? 'onload = \''. $ bodyLoad.' \'',''); ?> >

Already in my [action]. Ctp, I do the following:

<? php $ this-> set ('bodyLoad', 'field.focus ();');?>

If you want you can also put this code in the controller.

Good luck

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top