I know this is an old question, but this explanation can be really useful (I ruined my head this problem several times ..)
Let's start with the case:
You have 4 scripts to add to the page.
2 are shown in your layout and 2 in your view.
Name them script1, script2, script3 and script4 (the final order should respect the numbering).
Layout:
<?php $this->inlineScript()->prependFile('script2.js') ->prependFile('script1.js');
View:
<?php $this->inlineScript()->appendFile('script3.js') ->appendFile('script4.js');
Result:
<script type="text/javascript" src="/script1.js"></script> <script type="text/javascript" src="/script2.js"></script> <script type="text/javascript" src="/script3.js"></script> <script type="text/javascript" src="/script4.js"></script>
What happened
First, the view is processed, scripts (in the view) are added to the stack, so we have:
script3 added:
[script3.js] <--
script4 added:
[script2.js] [script3.js] <--
Then the layout is processed and the script is added in reverse order, therefore:
script2 added:
[script2.js] <-- [script3.js] [script4.js]
script1 added:
[script1.js] <-- [script2.js] [script3.js] [script4.js]
source share