Designing a proper view layout |
[eluser]yumology[/eluser]
I'm struggling terribly with creating a really well designed layout. All pages in my site have the following common php views loaded: head.php navbar.php footer.php My problem that I'm stuck on is where do I put the Javascript? At first I was putting it all in footer.php. But the problem there is that often I need to include javascript for one or more pages pages but rarely do I need to include javascript for all. For instance my contact us page has a modal that pops up and my index page has a carousel. Both of these require javascript to initialize and control. If I put the javascript in a common footer then it's loading on pages that don't need it. The more pages I'm making the bigger my javascript in footer.php is getting. This is causing cross page issues. I can't move the javascript up into the page body because jquery is included in footer. So I can't have a jquery function calls before jquery is defined. Should I make a separate javascript file for each page that needs javascript? Is that really the best method? Where would I put the js, in example.com/js or in application/views/js? How would I call it from CI? If I do that then I imagine my common items to be more like this: head.php navbar.php [PAGE BODY] footer.php (includes jquery file and navbar javascript) [EXTRA JAVASCRIPT if needed] htmlClosingTags.php That just seems to be less elegant. I like elegance and keeping it dry and doing it smart. What's the best way to make this look good?
[eluser]Aken[/eluser]
You should have one external javascript file with all of your JS code. Even if you don't need all of it on every page, it will be loaded once by the client (a viewer's browser, typically) and cached for each subsequent use. This file would then be added to just before the closing </body> tag (or sometimes in the header, depending on the JS used).
[eluser]yumology[/eluser]
[quote author="Aken" date="1360724555"]You should have one external javascript file with all of your JS code. [/quote] Aken I must be doing something majorly wrong then because this is more like what I'm dealing with: script src="/js/jquery-1.9.1.min.js" script src="/js/wysihtml5-0.3.0.js" script src="/js/bootstrap-wysihtml5.js" script src="/js/jquery.browser.js" script src="/js/jquery.iframe-auto-height.plugin.1.9.1.min.js" script src="/js/jquery.contactable.min.js" script src="/js/bootstrap.min.js" script src="/js/mysite-common.js" script src="/js/mysite-thispage.js" I can't even conceive of having one javascript file for my whole site...
[eluser]yumology[/eluser]
[quote author="Gonzalez Rivera" date="1360738742"]You should confirm to JS Code with one External JawaScript file. It will work and its 100% confirmed. [/quote] I don't now what this means. Can you elaborate in any way?
[eluser]Otemu[/eluser]
Hi, If you do take Aken method there is a number of ways to achieve this, one method can be found here http://www.monkeyphysics.com/articles/re...cript.html if you google am sure your find more. Another way is to just pass the JavaScript files you want in your controller as an array, something like: $data['myscripts'] = array('myscripts1.js','myscripts2.js', 'myscripts3.js'); and then in your footer just loop through these scripts foreach($myscripts as $scripts){ echo $scripts; } And a very simple method is to have a switch statement, depending on url or controller being used just load the files that way switch ($this->uri->segment(0)) { case 'news': echo "load news js"; break; case 'aboutus': echo "load company js"; break; case 'gallery': echo 'load my gallery js'; break; }
[eluser]edjon2000[/eluser]
Hi, I took a slightly different approach, I tend to use a template style layout for my views and add the javascript calls as required in my controllers. To explain this I have added an /includes folder to my views folder which contain a header.php(header) a template.php(main body) and a footer.php file which are constructed as follows. (header.php) Code: <!DOCTYPE html> Code: <?php $this->load->view('includes/header'); ?>
[eluser]edjon2000[/eluser]
and (footer.php) Code: ........ So to call a simple page, I would have this in my controller/method Code: <?php Code: $data['extra_head_content_n'] = 'path to my javascript files or javascript calls'; This approach came in handy when a client of mine wanted to be able to edit certain pages themselves so on those particular page controllers/methods I added js paths and calls to enable the tinymce editor. This may not be the best approach but it worked for me Jon
[eluser]yumology[/eluser]
edjon2000, that's great. I have adapted my code to be something very similar to yours. I am happy with this for now. Here is what it looks like: Code: <!DOCTYPE html>
[eluser]edjon2000[/eluser]
Glad it helped you out , as I said it is not necessarily the best way but for that particular site it worked fine, I also use a similar approach on some of the other sites I have constructed where loading speed is a consideration. Jon |
Welcome Guest, Not a member yet? Register Sign In |