CodeIgniter Forums
Javascript integration in views - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Javascript integration in views (/showthread.php?tid=8578)

Pages: 1 2


Javascript integration in views - El Forum - 05-23-2008

[eluser]Raf31[/eluser]
I'm developping my first application with Code Igniter. Everything is straightforwawrd and intuitive for me except one thing: the integration of Javascript in the views. I explain a bit: most of the times, the javascript I use is related to the design of the site so my feeling is that it should be included in the views and more particularly between the <head> tags of the HTML header. However, my view files look like this:

<? $this->load->view('front/header); ?>

The HTML code which is specific to this page.

<? $this->load->view('front/footer); ?>

So if I add Javascript somewhere before or after the first line, it will not be in the HTML header. Do you have some "tricks" to do this properly? Thanks.


Javascript integration in views - El Forum - 05-23-2008

[eluser]Michael Wales[/eluser]
umm... just put it in your header view file?

front/header.php
Code:
<html>
  <head>
    <title>My Title</title>
    <!-- Put your script tag here - the forums cut it out -->
  </head>



Javascript integration in views - El Forum - 05-23-2008

[eluser]Raf31[/eluser]
umm ... the issue is that my javascript is specific to each page so I cannot include it in the header view file.


Javascript integration in views - El Forum - 05-23-2008

[eluser]Michael Wales[/eluser]
I'm still not sure how that makes a difference - without seeing what your JS looked like - sounds to me to be some obtrusive coding. Personally, I'd take a look at my Javascript and figure out why it can't be shared across all pages.

But - if you want, you could do something like this within each controller:
Code:
$data['js_file'] = 'index.js';

Then in your header view:
Code:
script src="<?= base_url(); ?>assets/js/<?= $js_file; ?>" type="text/javascript"

By loading the header and footer from your page specific view you are automatically passing the $data array to those views (without even changing the load->view() method).


Javascript integration in views - El Forum - 05-23-2008

[eluser]Raf31[/eluser]
Thank you Michael for your time! I've seen this method (probably on your blog by the way!) and I agree it is good for Javascript files, but I was rather talking of Javascript code such as the one you can use for JQuery:


$(document).ready(function() {
$('#myForm').slide();
});


This code is highly dependent on the page view, so in my opinion, it should be included in the view file but I don't find a "clean" way to do so.


Javascript integration in views - El Forum - 05-23-2008

[eluser]Michael Wales[/eluser]
ah - see, now we're tracking!

The same principle would apply: assign that string of javascript code to a variable, then echo it out between SCRIPT tags within the head.

Code:
$data['page_js'] = '$(document).ready(function() {
$(\'#myForm\').slide();
});';

Then echo out that var between SCRIPT TYPE="text/javascript" tags.


Javascript integration in views - El Forum - 05-23-2008

[eluser]xwero[/eluser]
FYI, after the yslow benchmark came out developers start putting the javascript code/links near the bottom of the page. It speeds up the display of information on the page.


Javascript integration in views - El Forum - 05-23-2008

[eluser]Popcorn[/eluser]
^ Unless you provide hard facts I wont believe it Tongue


Javascript integration in views - El Forum - 05-23-2008

[eluser]xwero[/eluser]
[quote author="Popcorn" date="1211555854"]^ Unless you provide hard facts I wont believe it Tongue[/quote]
I leave the facts to the benchmark specialists, leaves me more time for coding apps :coolsmile:


Javascript integration in views - El Forum - 05-23-2008

[eluser]Michael Wales[/eluser]
hard facts