Parsing php in javascript - 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: Parsing php in javascript (/showthread.php?tid=37029) |
Parsing php in javascript - El Forum - 12-24-2010 [eluser]Maglok[/eluser] Alright this is going to get a little complicated so bear with me. I have several subdomains each with their own index.php in those index.php's I define what skin the site uses. So skin1.domain.com and skin2.domain.com have the proper colors. Each template has a different css. Easy enough. I use carabiner to combine css and javascript (css and js thus each being their own file) to reduce the amount of http requests. I am now in the process of streamlining the coding. At the moment I have snippets of JS code for each script I need initiated. Since not every page needs every snippet I am moving each snippet to a javascript file of it's own which I include on a page by page basis with carabiner. For example this is how the tinyMCE init looks like: Code: [removed] You will notice I echo APP_MAIN and APP_SITE. Those are globals that don't get parsed in the javascript file if it is not inline. I googled around some and people seem to suggest to tell apache to parse javascript by php like so: Code: AddHandler application/x-httpd-php .js This doesn't work. I am even bypassing carabiner for it at the moment. TinyMCE does initialize, but it isn't taking the php echo-ing. I could of course create a library or controller or so that outputs the correct code snippets, but I really rather keep all of that out of CI and just in a javascript scripts dir. Anyone follow what I am trying to achieve? Parsing php in javascript - El Forum - 12-24-2010 [eluser]cahva[/eluser] Well the easiest to do here would be to add a little inline js where you define APP_MAIN and APP_SITE before loading the scripts. So inside your layout/header view something like this should work: Code: <html> Code: content_css : APP_MAIN + "css/" + APP_SITE + ".css", Parsing php in javascript - El Forum - 12-24-2010 [eluser]Maglok[/eluser] Yeah that wouldn't work. Cause the whole point is to not have to load script specific code on pages that don't use the code. It is just two, I know, but there has to be a way around this. Thanks though. Parsing php in javascript - El Forum - 12-28-2010 [eluser]eokorie[/eluser] Hi Maglok, I had a similar issue recently and the only way I was able to get this to work was to encode the base_url and the APPPATH into a json array and access them through javascript. Hope that gives you an idea Parsing php in javascript - El Forum - 12-29-2010 [eluser]Atharva[/eluser] The workaround will be store your js code in php file something like my_js.php Code: [removed] load it with Code: <script type="text/javascript" src="http://domain/my_js.php"></script> have you tried this? |