Welcome Guest, Not a member yet? Register   Sign In
External JS with CI / PHP
#1

[eluser]mjstenbe[/eluser]
I moved all my JS code to an external file. Now I have issues with AJAX functions referring to <?php echo base_url();?>. Any good advice on how to deal with these? I'd rather not hardcode any paths to my application.

What would be the ideal location for external JS and CSS files? Now Im using /system/application/libraries?

Any tips are welcome.

Thanks,
Mika
#2

[eluser]Maglok[/eluser]
Outside of CI.

./
./css
./scripts
./system

The base_url() should work then. You can't reference them as libraries from within CI. Reference them so:

Code:
<?php echo base_url();?>css/stylesheet.css
#3

[eluser]mjstenbe[/eluser]
Thanks for the input Simon. This doesnt solve the problem though.

The problem being <?php echo base_url();?> - statement in the external JS file. And thus the function not working properly. Placed in the PHP file this works fine. See an example below.


Code:
$.ajax({
        type: "POST",
        url: "<?php echo base_url();?>index/test/delete",
        data: data,
        success: function(msg) {
   // // //
   });
Is it really the case, that in order to use JS with PHP, it has to be contained within the same file?

Thanks,
Mika
#4

[eluser]Phil Sturgeon[/eluser]
Code:
var BASE_URI = "<?php echo base_url(); ?>";

Put that in your <head> tag before you include any JS.
#5

[eluser]coolgeek[/eluser]
I use absolute pathing and it works just fine

Code:
url: "/ajax_functions/ac_get_city",
#6

[eluser]Phil Sturgeon[/eluser]
That works fine until you move your application into a directory. You might think that doesn't happen very often but you'd be surprised how many developers still work on localhost without virtual hosts.

I actually generate my absolute paths in constants.php then use that value everywhere.

Code:
/*
|--------------------------------------------------------------------------
| Docment root folders
|--------------------------------------------------------------------------
|
| These constants use existing location information to work out web root, etc.
|
*/

// Base URL (keeps this crazy sh*t out of the config.php
if(isset($_SERVER['HTTP_HOST']))
{
    $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
    $base_url .= '://'. $_SERVER['HTTP_HOST'];
    $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
    
    // Base URI (It's different to base URL!)
    $base_uri = parse_url($base_url, PHP_URL_PATH);
    if(substr($base_uri, 0, 1) != '/') $base_uri = '/'.$base_uri;
    if(substr($base_uri, -1, 1) != '/') $base_uri .= '/';
}

else
{
    $base_url = 'http://localhost/';
    $base_uri = '/';
}

// Define these values to be used later on
define('BASE_URL', $base_url);
define('BASE_URI', $base_uri);
define('APPPATH_URI', BASE_URI.APPPATH);

// We dont need these variables any more
unset($base_uri, $base_url);

Pass those to JS with:

Code:
var BASE_URI = "<?php echo BASE_URI; ?>";

then you are cooking with gas :-)
#7

[eluser]Maglok[/eluser]
[quote author="mjstenbe" date="1268848621"]Thanks for the input Simon. This doesnt solve the problem though.

The problem being <?php echo base_url();?> - statement in the external JS file. And thus the function not working properly. Placed in the PHP file this works fine. See an example below.


Code:
$.ajax({
        type: "POST",
        url: "<?php echo base_url();?>index/test/delete",
        data: data,
        success: function(msg) {
   // // //
   });
Is it really the case, that in order to use JS with PHP, it has to be contained within the same file?

Thanks,
Mika[/quote]

You did mention that you put it inside the CI dir at first, that should not work by default. Smile But yes define constant in the head, e voila.
#8

[eluser]Jeremy Gimbel - Conflux Group[/eluser]
I've been known to do a little trickery with .htaccess and tell Apache to interpret .js files as PHP. It may be considered a mortal sin in some circles, so I wouldn't recommend it unless you really know what you're doing here.

Something like this does the trick though:

Code:
<FilesMatch "\.(js)$">
AddHandler application/x-httpd-php .js
</FilesMatch>

Doing this allows you to echo out variables directly in your JavaScript files.
#9

[eluser]Zeeshan Rasool[/eluser]
@phil Sturgeon, nice solution. I also did like that.
Just declare all these path or values at your head section by assigning these to JS variables.
var siteUrl = '&lt;?php echo base_url();>';




Theme © iAndrew 2016 - Forum software by © MyBB