开发者

Add to <body> tag of a cakePHP app

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


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; ?>">


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜