Welcome Guest, Not a member yet? Register   Sign In
How create a JavaScript file with a view in CodeIgniter?
#1

[eluser]ivarsmas[/eluser]
Hello everyone,

I have to create a js dynamically file (calling a database and getting values) with CI and i don't know how, if i use a view the result is a php file, but I need to make this a js file, because i must be embed it on other pages.

I have searched in this board and haven't find information.

Any suggestion?

Thanks!!
#2

[eluser]Zack Kitzmiller[/eluser]
I'm not sure what you mean exactly, but if you need to use your PHP retrieved values in a JS script, then you can just do this:

Code:
[script type="text/javascript"]
var 1 = <?= $value['result_1']; ?>;
var 2 = <?= $value['result_2']; ?>;
[/script]

<!-- Static JS Here -->
[script type="text/javascript" src="<?base_url()?>js/script.js][/script]

This will give you access to your php assigned vars inside of your JS code.

(js should be <> not []
#3

[eluser]devbro[/eluser]
what about ajax? if you decided to go this way, you should right a special view file that spit outs the correct type of output that ajax expects (plain text, xml, ...)
#4

[eluser]Zack Kitzmiller[/eluser]
No, I don't think that is the case at all.

For adding ajax, just populate your variables before load your customer tool, and build a controller to handing ajax calls.

I usualy just use ajax.php for the controller, and build my methods in there.
#5

[eluser]ivarsmas[/eluser]
Thanks techneke, but maybe I have not explained well. :-S

I need create a JavaScript file with a ".js" extension, but with the "views of CodeIgniter" only is possible output a result without extension and executed in own server.

With a CI view now i get:
http://www.domain.com/order

And i want get:
http://www.domain.com/order/js_file_name.js?id=11111 (o similar..) and changing his content by calling to database with this id passed.

For i can to give to users this js file, to embed this in his sites once created. And execute it in navigator of user.

It is better explained now?
#6

[eluser]Zack Kitzmiller[/eluser]
first of all, why are you passing things into js that way? Bad Practice..

Second, then call it like this

[script type="text/javascript" src="&lt;?= base_url(); ?&gt;js/jsfile.js?id=&lt;?= $what_ever_php_var; ?&gt;"][/script]
#7

[eluser]ivarsmas[/eluser]
The problem isn't calling to javascript, else to create the file with js extension how output in CodeIgniter.
#8

[eluser]Zack Kitzmiller[/eluser]
There is no reason to build a JavaScript file in that way.

I'd highly recommend rethinking the way you're executing your project.
#9

[eluser]davidbehler[/eluser]
Lets say you have a controller called "js" and a function "get"
Code:
class js extends Controller
{
   function js()
   {
      parent::Controller();
   }

   function get($id = 0)
   {
      $data = array();
      $data['test'] = $this->Test_model->get_test($id);
      $this->output->set_header("Content-type: text/javascript");
      $this->load->view('js_file_view', $data);
   }
}

Now you can include the file using
Code:
[script type='text/javascript” src=”&lt;?=site_url('js/get/123');?&gt;”][/script]
#10

[eluser]Zack Kitzmiller[/eluser]
You'd have to do some routes stuff with that kind of execution, some browers will choke on not having a .js extension. Even with the correct header.




Theme © iAndrew 2016 - Forum software by © MyBB