Javascript – Zend headScript() and appendFile not working as expected

appendfilejavascriptzend-framework

I'm having an issue trying to append a javascript file using headScript()->appendFile('file name') with Zend. I have my layout setup like this:

    <?= $this->headScript()
    ->prependScript( 'BASE_URL = "' . $this->baseUrl() . '";' )
    ->appendFile( $this->baseUrl('js/lib/jquery/jquery-1.4.2.min.js') )
    ->appendFile( $this->baseUrl('js/admin.js') );

?>

Then, in my controller I am trying to append an additional js file only for this page, like:

    $this->view->headScript()->appendFile( 'another/js/file.js' );

This file needs to be appended to what is already setup in the layout. However, this file gets added before the other 'appendFile' files. I've also tried

$this->headScript()->offsetSetFile(999, '/js/myfuncs.js');

But this still adds the file before the other files. This is not how I would expect this to work, especially when using the offsetSetFile method. How can I add this file after the other files? Thanks.

Best Answer

The answer is to use headScript()->prependFile in layout.phtml and headScript()->appendFile in a view.