title: Loading javascript in Code Igniter
body: <p>For our Code Igniter application we are loading all of our javascript just before the closing body tag.</p>
  
  <p>so in our controllers we have</p>
  
  <pre><code>$this-&gt;load-&gt;view('head', $this-&gt;head);
  $this-&gt;load-&gt;view('main_view_for_controller', $data);
  $this-&gt;load-&gt;view('foot', $this-&gt;foot);
  </code></pre>
  
  <p>And in the foot view we have a bunch of <code>&lt;script src="master.js"&gt;&lt;/script&gt;</code> tags.</p>
  
  <p>These include</p>
  
  <ul>
  <li>jQuery</li>
  <li>jQuery-ui</li>
  <li>shared-functions</li>
  </ul>
  
  <p>Now this works great, until you think about JS used only on specific pages, or inline js.<br>
  You can't just dump your javascript anywhere in the page as it will generally use bit's and pieces of the parts you load at the bottom.</p>
  
  <p>So what I do at the moment is,<br>
  I have <code>/application/views/js/</code> where I will have something like <code>login.php</code></p>
  
  <p><code>login.php</code> may contain e.g.</p>
  
  <pre><code>&lt;script&gt;
  $(function(){
  
      var user_id = &lt;?php echo $this-&gt;user-&gt;get('id'); ?&gt;;
      var return = '&lt;?php echo $return_url; ?&gt;';
  
      $('#login form').submit(function(){[...]});
  
      $('#login .facebook').click(function(){[...]});
  });
  &lt;/script&gt;    
  </code></pre>
  
  <p>so in my controller I would call<br>
      <code>$this-&gt;foot['js'][] = javascript('login', array('return_url' =&gt; '/users'));</code></p>
  
  <pre><code>//source of function javascript() from inside a helper
  function javascript($file, $config = array()){
      return $this-&gt;load-&gt;view('js/'.$file, $config, true);
  }
  </code></pre>
  
  <p>then in my foot view after all the other files (which on the prod env are merged into one file and then minified) I have</p>
  
  <p><code>foreach($js as $jsOut) echo $jsOut;</code></p>
  
  <p>which simply spits out all the javascript.</p>
  
  <p>Is this the best way of doing this? or is there a better way?
  This just seems kind of messy...</p>