CodeIgniter Forums

Full Version: how to get handle to CI application directory from within a js file?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]fchristant[/eluser]
hi,

I'm thoroughly enjoying using CI for a large project. I've learned a lot from practice, the documentation and forum but still have quite a simple question...

Inside my javascript files, how can I refer to the CI application path? Here's some of my code:

Code:
var postURL = "<?php $CI =& get_instance(); $CI->load->helper('url'); site_url("json_username_check"); ?>/" + username + "/" + new Date().getTime();
        $.ajax({
            type: "POST",
               url: postURL,
               success: function(msg) {
                   result = (msg=='TRUE') ? true : false;
               }
         });

It's about the first line really. As you can see I'm trying all sorts of things to get a handle to CI's application path from within javascript. Compliant with best practices I have the js files inside the webroot (js dir) and the CI files outside the webroot. From my views I reference the JS files and this works fine. I have the following .htaccess inside the webroot:

Code:
RewriteEngine on
RewriteRule ^$ /apps/OS4W/html/index.php [L]
RewriteCond $1 !^(index\.php|images|img|css|js|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /apps/OS4W/html/index.php/$1 [L]

And the following .htaccess in the webroot/js dir:

Code:
AddHandler application/x-httpd-php .js
php_value auto_prepend_file gzip-js.php

This last .htaccess is not really relevant for my question, it's purpose is to automatically compress my js files.

As mentioned, the problem is here:

Code:
var postURL = "<?php $CI =& get_instance(); $CI->load->helper('url'); site_url("json_username_check"); ?>/" + username + "/" + new Date().getTime();

The site_url function is not a valid function, it is not recognized. Loading the URL helper does not help either. I also cannot get an instance to the CI object. I suspect this is due to the .htaccess but I'm not sure how to fix this without having other unwanted impact.

Thanks in advance for anyone offering help Smile

El Forum

[eluser]fchristant[/eluser]
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:

[removed]
var basepath = "<?= site_url(); ?>";
[removed]

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?