Welcome Guest, Not a member yet? Register   Sign In
Javascript integration in views
#1

[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.
#2

[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>
#3

[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.
#4

[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).
#5

[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.
#6

[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.
#7

[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.
#8

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

[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:
#10

[eluser]Michael Wales[/eluser]
hard facts




Theme © iAndrew 2016 - Forum software by © MyBB