How create a JavaScript file with a view in CodeIgniter? |
[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!!
[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"] This will give you access to your php assigned vars inside of your JS code. (js should be <> not []
[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, ...)
[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.
[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?
[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="<?= base_url(); ?>js/jsfile.js?id=<?= $what_ever_php_var; ?>"][/script]
[eluser]ivarsmas[/eluser]
The problem isn't calling to javascript, else to create the file with js extension how output in CodeIgniter.
[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.
[eluser]davidbehler[/eluser]
Lets say you have a controller called "js" and a function "get" Code: class js extends Controller Now you can include the file using Code: [script type='text/javascript” src=”<?=site_url('js/get/123');?>”][/script]
[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. |
Welcome Guest, Not a member yet? Register Sign In |