• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to get handle to CI application directory from within a js file?

Just wanted to give an update to my own question. Until I find a better solution, I've settled on the following approach, which works:

Each page that is loaded from my controller will also load a generic header view. As part of this header view, I have the following code:

var basepath = "<?= site_url(); ?>";

Note: it seems the forum validation removes my script tags, but you know what I mean.

I'm basically defining basepath as a global JS variable. This way, JS files outside the CI directory can also refer to this variable. I works, but it is not pretty. Neither are the other options I saw:

- using a hidden field on each page and fill it with the base url. Next, read it's value from JS. Kind of similar to my own solution, yet even nastier I think as it requires a form on each page. Both solutions inject unwanted markup.

- using hooks to dynamically inject (or replace placeholders) php vars into js files. Could work but it does not feel intuitive in terms of maintenance me thinks.

- placing global JS config vars in a seperate JS file and reference it from all other js files. The problem here is that my js files are outside the CI path (as they should be) thus these js vars cannot be set dynamically. This would result in a second config file (next to the config file for CI) to maintain application paths. This can be overcome by implementing a controller method that outputs the CI config settings as JSON, but to me it seems like a very heavy approach. Calling a remote request on each page just to get an application path???

- finally, avoid the entire problem and place js files inside the CI directory (in the views dir). I'm considering doing this, as it makes all the compromises mentioned above unneeded and I can have clear markup. I know this goes against best practices but what are the real risks, problems, and impact when choosing this path?

Messages In This Thread
how to get handle to CI application directory from within a js file? - by El Forum - 11-26-2008, 03:16 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.