0

I am using php in my javascript file and my .htaccess looks like

<FilesMatch "\.css$">
  SetHandler application/x-httpd-php
  Header set Content-type "text/css"
</FilesMatch>

<FilesMatch "\.js$">
  SetHandler application/x-httpd-php
  Header set Content-type "application/javascript"
</FilesMatch>

I can now use php from within my javascript files but codeigniter functions do not work.For example

$('#example').dataTable( {
        "bProcessing": true,
        "sAjaxSource": "<?php $CI = get_instance(); echo base_url(); ?>"
    } );

gives the error

<b>Fatal error</b>:  Call to undefined function get_instance() in <b>C:\xampp\htdocs\_init\assets\js\custom.js</b> on line <b>88</b><br />

I figured maybe i need to load the codeigniter environment in a way similar to the wordpress require( '../wordpress/wp-load.php' );

Is there a way i can load codeigniter in a similar manner so that codeigniter functions are available to my javascript and css files?.

3
  • You should also make sure you specifically send no-cache headers along with these "files" so that the browser doesn't cache the CSS an js thinking that the files are typical static files. Of course this could significantly impact your website performance from the user's standpoint, so really I would question the whole approach to begin with. For example, there is not reason in the JS snippet, you couldn't define a variable in JS global scope for sAjaxSource that could be referenced in a static js file.
    – Mike Brant
    Aug 6, 2013 at 17:28
  • Thanks for your comment.I have reconsidered the approach and abandoned the plan all together.In its place,i will just i will just load what i need via the url "sAjaxSource": 'http://localhost/_init/assets/static_content/arrays.txt' Aug 6, 2013 at 21:04
  • I liked both users SO name :| thats coooolll!
    – Nono
    Jul 11, 2017 at 19:45

1 Answer 1

3

CI relies on the bootstrap system/core/CodeIgniter.php to kick off the application..

There isn't a magic file that you can include that will give you access to the functions..

Solution 1: The best solution for your base_url problem is to add a base tag as described by Phil Sturgeon..

<base href="<?php echo base_url(); ?>">

Solution 2: define a js base_url in a view

<script>
   var base_url = "<?php echo base_url(); ?>";
</script>

In your js you would access it like..

$('#example').dataTable( {
    "bProcessing": true,
    "sAjaxSource": base_url
});

What you are asking for:

If you rely on CI functions in your js and css, then your approach should be revisited, nevertheless, here is a possible solution..

Create a controller application/controllers/js.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class js extends CI_Controller {
    public function index()
    {
        $this->load->view('js');
    }
}

Create a view application/views/js.php

$('#example').dataTable( {
    "bProcessing": true,
    "sAjaxSource": "<?php echo base_url(); ?>"
});
1
  • Since the suggested edit would probably be rejected, you can find an improved version of the third solution here. Aug 6, 2013 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.