Search code examples
zend-frameworklayouthead

How to add script files using headScript with Zend Framework?


I am developing a new zend framework application, and when I just hard coded the headScript section for my layout.phtml file and Bootstrap _initScript method, when looking at the source-code after not having the script loaded... a got a really weird result.

I got this:

<script type="text/javascript">
    //<!--
    /js/jquery-1.8.1.min.js    //-->

Here is my layout.phtml code:

<?php echo $this->doctype(); ?> 
<html>
    <head>
        <?php echo $this->headTitle(); ?>
        <?php echo $this->headLink(); ?>
        <?php echo $this->headScript(); ?>
    </head>
    <body>
        <?php echo $this->layout()->content; ?>
    </body> 
</html>

Here is my Bootstrap.php code:

protected function _initScript()
{
    $this->view->headScript()
       ->prependScript( "/js/jquery-1.8.1.min.js" );
}

As you can see it is very ordinary code!

Anybody could help me find out what's going on here?


Solution

  • You are trying to add "file" not "script".

    Change Bootstrap.php to:

    protected function _initScript()
    {
        $this->view->headScript()
           ->prependFile( "/js/jquery-1.8.1.min.js", $type = 'text/javascript' );
    }
    

    Also check this for headScript reference.

    Any questions? :)