![]() |
javascript and codeigniter - 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 and codeigniter (/showthread.php?tid=17762) |
javascript and codeigniter - El Forum - 04-15-2009 [eluser]hudar[/eluser] Hi, good day.. I just wonder if anyone have some idea regarding this case : I have javascript code like below : Code: $.post( I wrote that javascript on view which is php file. I wonder if this javascript could be made separated as .js file, so we just need to import that .js file in every page when needed, instead of copy paste all the javascript on every page (very paintfull ![]() Since my php page on view, contain a lot of javascript code. The javascript use php code which is <?=site_url()?>, does this code will work if separated as .js file? I don't think so. Any suggestion would be very appreciated. javascript and codeigniter - El Forum - 04-15-2009 [eluser]rogierb[/eluser] Why not put the javascript in another view file and load that whenever you need it? javascript and codeigniter - El Forum - 04-15-2009 [eluser]hudar[/eluser] Hi Rogierb, Did you mean put the javascript as another php file, then include that php file? Never tried it, but will consider this. But if there is any other options, I would like to hear that. javascript and codeigniter - El Forum - 04-15-2009 [eluser]Jameson.[/eluser] Well, you could make Apache treat those .js files as php scripts by adding a handler similar to this: Code: AddHandler application/x-httpd-php .js We'll have to refactor js code a bit: Code: function startup(url, date) Code: < script type='text/javascript'> On a side note: personally I prefer to put all my static client-side resources in one folder, say "static", and then just refer it via relative path from domain root (don't know how to call it correctly in English), like this: Code: < script type='text/javascript' src='/static/js/common.js'>< /script> This way I don't have to manually change references to files when using on another project and don't need to provide URL in program. javascript and codeigniter - El Forum - 04-15-2009 [eluser]slowgary[/eluser] Hudar, you can link to a PHP file instead of JS, like so: Code: < script type='text/javascript' src='ajaxscript.php' ></ script > Then in ajaxscript.php just send the header: Code: Header("content-type: application/x-javascript"); And then echo your javascript. Just make sure it echo's valid JS, otherwise just like any JS file, you'll get errors. |