title: Load Javascript files in CakePHP Layout at "bottom"
body: <p>Currently, I have a big CakePHP application with a layout and a lot of views. In the layout, I load Javascript files in the head which are needed by most views. In the views themself, I either load additional Javascript files (e.g., load a library file that is only needed there), or I add some Javascript code that is only relevant for this view in a script tag, for example if I need a click handler.</p>
  
  <p>Since it is a known fact that loading Javascript files in the HTML head blocks loading the page, I wanted to put them at the bottom before closing the body Tag. But if I do so, the Javascript in my views that load the content breaks because it does not know about my loaded Javascript files. I understand that the Javascript code in the loaded views is executed before my files are loaded. But how can I prevent that? </p>
  
  <p>I'm currently using the HTML Helper in the Layout (and everywhere else) to load my JS files like this:</p>
  
  <pre><code>&lt;?php echo $this-&gt;Html-&gt;script('//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'); ?&gt;
  </code></pre>
  
  <p>And I use the JS Helper to "output" JS at the end of the page with</p>
  
  <pre><code>&lt;?php echo $this-&gt;Js-&gt;writeBuffer(); ?&gt;
  </code></pre>
  
  <p>Is there a way I can append my JS code in the views, so that it is executed when I call writeBuffer? Can that help me out? </p>