Welcome Guest, Not a member yet? Register   Sign In
Best way to setup dynamic js file => javascript.php
#1

[eluser]iKode[/eluser]
I need to create a few dynamic javascript files that contains information from the db. What would be the correct way to do this with codeigniter. Normally I would create a javascript.php file with javascript headers and grab the information directly from the database.
#2

[eluser]darrentaytay[/eluser]
You can do the same with Codeigniter. Just create a function inside a controller and link to it as you would a .php file.

Alternatively, you can create a function which actually writes the javascript to a .js file and reference that.
#3

[eluser]NeoArc[/eluser]
Code:
<head>
<s-c-ri-p-t>
  //These variables will be available in external files (including functions)
  var SITE_BASE_URL = "&lt;?php echo base_url()?&gt;";
  var SITE_TIME = &lt;?php echo time()>;
</s-c-ri-p-t>
<s-c-r-i-pt s-rc="external-file-process-time.js" ></s-cr-i-p-t>
&lt;/head&gt;
#4

[eluser]Unknown[/eluser]
I'm trying to do this and Actually works but I get the following error "Cannot modify header information - headers already sent by ...system/core/Output.php 276".

What could be wrong in the following code in the controller function (/site/lang_js)?

[code]
function lang_js() {

$this->output->set_header('Cache-Control: no-cache, must-revalidate');
$this->output->set_header('Exprires: Mon, 4 April 1994 04:44:44 GMT');
$this->output->set_header('Content-type: application/x-javascript');



$this->lang->load('messages',$this->session->userdata('language'));
$this->lang->load('user',$this->session->userdata('language'));

echo "var lang = " . json_encode($this->lang->language);

}
[code]

Thanks!
#5

[eluser]osci[/eluser]
don't echo
do
Code:
$this->output->set_output("var lang = " . json_encode($this->lang->language));

Although when I want to serve a js file I usually do
Code:
header('Content-Type: text/javascript; charset=UTF-8');
echo $output;




Theme © iAndrew 2016 - Forum software by © MyBB