Using JQuery with CodeIgniter
Monday, July 6th, 2009Using JQuery with CodeIgniter is simple. In fact, using most Ajax frameworks with CodeIgniter is easy. I created a simple JQuery plugin that displays randomly selected images (imBannerRotater).
First I use CodeIgniter’s ‘base_url’ function to link the necessary Javascript files in a main view of my application:
<script type="text/javascript" src="js/jquery/jquery-1.3.1.min.js"></script> <script type="text/javascript" src="js/jquery/jquery.imBannerRotater-0.5.js"></script> |
Next, I define a Javascript variable to be used with by the plugin:
<script type="text/javascript"> //<![CDATA[ base_url = '<?= base_url();?>'; //]]> </script> |
I then use this variable in my JQuery Plugin:
$(document).ready(function(){
$(".randPic").imBannerRotater({
data_url: base_url + 'assets/sidebar/sidebar.php',
base_path: base_url + 'assets/sidebar/'
});
}); |
That’s about it. While this is a simple example, using CodeIgniter’s base_url function is the key to using JQuery or any other Ajax Framework.



July 7th, 2009 at 10:41 am
I think base tag is another good solution for this.
http://www.w3schools.com/TAGS/tag_base.asp
DominixZ´s last blog ..[MV]Tell Me Your Wish – Girl’s Generation(SNSD) พร้อมซับไตเติลไทย =-.
September 1st, 2009 at 7:09 am
Thanks. I have seen some examples with base_url inside CDATA and I was wondering, why is this. You post clear this out.
Thanks
Márcio
September 1st, 2009 at 9:22 am
This link explains the use of CDATA better than I can:
http://javascript.about.com/library/blxhtml.htm
September 1st, 2009 at 10:01 am
I’ve see it as well. Quite good article. And a good “ensemble” with yours.
Regards,
Márcio