javascriptcakephp-2.7

cakePHP scriptStart() and scriptEnd(), how to use?


why this code is not working? Seemsm like i don't understand the usage of scriptstart() and scriptEnd().

// view
<?php
  $this->Html->scriptStart(array("block"=>true,"inline"=>FALSE));
?>
$().ready(function(){
    alert("dd");
});
<?php 
  $this->Html->scriptEnd();
?>
// layout
echo $this->fetch('script');

edit

Some more info:

  1. I expect it to popup the alert..
  2. In the example nothing happens. seems like the javascript is not being added to the page. (i checked the source)

Solution

  • Try removing the "block"=>true option, or setting it to 'script' instead:

    // view
    <?php
      $this->Html->scriptStart(array("block"=>'script',"inline"=>FALSE));
    ?>
        $().ready(function(){
            alert("dd");
        });
    <?php 
      $this->Html->scriptEnd();
    ?>
    

    Make sure you include the php tags in the layout:

    // layout
    <?php echo $this->fetch('script');?>