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

[eluser]slowgary[/eluser]
I don't think browsers care about the extension. If browsers cared about extensions, they'd get choked up on query strings and such. I think it would be counter-productive for a browser developer to restrict web software to certain extensions, as they would then just have to do more work when a new extension became popular. Think .htm, .html, .shtml, .chm, .cfm, .php, .asp - they all provide the browser with markup. Not to mention, there is NO BENEFIT to restricting file extensions. They basically exist for the developer to differentiate and for operating systems that rely on the file extension to determine the file type.

That being said, I think you can name your function like this:
Code:
function filename_js()
{
}
and still access it via the URL like this:
Code:
http://www.yourdomain.com/controller/filename.js?id=12345

As least on my CodeIgniter install it translates periods to underscores. You'd need to enable query strings in CodeIgniter to be able to do this, obviously. Then you just do your database query and echo valid JavaScript and you should be fine.
#12

[eluser]Zack Kitzmiller[/eluser]
Yeah, I suppose that would work in most situations. I know that in the past, I've had issues getting IE 6 to cooperate with not standard file extensions for certain embedded scripts.

Still, all being said and done, I don't think that is would ever be the best way to handle this situation, but then again, I'm not familiar with the person's exact situation.
#13

[eluser]slowgary[/eluser]
It sounds like an okay way to me. It looks like ivarsmas intends on allowing third party applications to include a javascript file from his site, but in it he needs to include some code that is specific to that site or user. It could also be done using a second script tag, like so:
Code:
< script type='text/javascript' >
var ivarsmas_id = '12345';
< /script >
&lt; script type='text/javascript' src='http://www.ivarsmas.com/filename.js' &gt;&lt; /script >
Then your script could check the value of that variable and do what it needs to do. The problem with this method though is that not only is it more complicated for the third party, but you'd need to check the variable then do your dynamic processing with AJAX, which is all just more overhead. I prefer the single include"
Code:
&lt; script type='text/javascript' src='http://www.ivarsmas.com/filename.js?id=12345' &gt;&lt; /script >
Easy and clean.
#14

[eluser]Bernat Bonet[/eluser]
I think my problem is the same, then I'll explain it:
I want to generate js file from php file, I mean, I've php file that generate js file calling database and codeigniter core.
The generated file have to be accesed from view.
The problem is how I generate js file from interpreted php file ?

View:
Code:
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;&lt;?php echo $this->lang->line('login_titulo'); ?&gt;&lt;/title&gt;
    &lt;link rel="stylesheet" type="text/css" href="/lib/extjs3.0/resources/css/ext-all.css" /&gt;
    [script src="/lib/extjs3.0/adapter/ext/ext-base.js" type="text/javascript"][/script]
    [script src="/lib/extjs3.0/ext-all.js" type="text/javascript"][/script]
    [script src="/lib/extjs3.0/src/locale/ext-lang-&lt;?= $this->config->item('language');?&gt;.js" type="text/javascript"][/script]
    [script src="login/login.php" type="text/javascript"][/script]
    [script src="login/login_base.js" type="text/javascript"][/script]
  &lt;/head&gt;
  &lt;body&gt;
  &lt;/body&gt;
&lt;/html&gt;

PHP file to generate js file (login.php):
Code:
var login_form = new Ext.form.FormPanel({
  labelWidth   : 150,
  url          : 'login/validar',
  frame        : true,
  title        : '',
  width        : 230,
  padding      : 20,
  defaultType  : 'textfield',
  monitorValid : true,
  buttonAlign  : 'center',
  items : [{
    fieldLabel : '&lt;?=$this->lang->line("login_login_form_field_usename");?&gt;',
    name       : 'username',
    allowBlank : false
  }, ...

Where to place php file, and how can we generate js/php file ?

Thanks a lot.
#15

[eluser]davidbehler[/eluser]
Look at my post, it pretty much does what you need.
#16

[eluser]Zack Kitzmiller[/eluser]
[quote author="Bernat Bonet" date="1248197833"]I think my problem is the same, then I'll explain it:
I want to generate js file from php file, I mean, I've php file that generate js file calling database and codeigniter core.
The generated file have to be accesed from view.
The problem is how I generate js file from interpreted php file ?

View:
Code:
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;&lt;?php echo $this->lang->line('login_titulo'); ?&gt;&lt;/title&gt;
    &lt;link rel="stylesheet" type="text/css" href="/lib/extjs3.0/resources/css/ext-all.css" /&gt;
    [script src="/lib/extjs3.0/adapter/ext/ext-base.js" type="text/javascript"][/script]
    [script src="/lib/extjs3.0/ext-all.js" type="text/javascript"][/script]
    [script src="/lib/extjs3.0/src/locale/ext-lang-&lt;?= $this->config->item('language');?&gt;.js" type="text/javascript"][/script]
    [script src="login/login.php" type="text/javascript"][/script]
    [script src="login/login_base.js" type="text/javascript"][/script]
  &lt;/head&gt;
  &lt;body&gt;
  &lt;/body&gt;
&lt;/html&gt;

PHP file to generate js file (login.php):
Code:
var login_form = new Ext.form.FormPanel({
  labelWidth   : 150,
  url          : 'login/validar',
  frame        : true,
  title        : '',
  width        : 230,
  padding      : 20,
  defaultType  : 'textfield',
  monitorValid : true,
  buttonAlign  : 'center',
  items : [{
    fieldLabel : '&lt;?=$this->lang->line("login_login_form_field_usename");?&gt;',
    name       : 'username',
    allowBlank : false
  }, ...

Where to place php file, and how can we generate js/php file ?

Thanks a lot.[/quote]

Again, I would do it differently. But no one really seems worried about preformance and number of assets loading anymore.
#17

[eluser]slowgary[/eluser]
@techneke:

Does dynamically generated JavaScript have anything to do with the number of assets being loaded? The number of assets being loaded could be just one - the dynamically generated JavaScript file.

Also, why does this method perform poorly? Using two script tags as you've suggested still requires a call to the database, and still requires the PHP to process it. The only difference with your method is that it adds further client-side overhead in that it requires an additional AJAX call, and it increases the total loading time of the page as the user has to wait for the page to load, THEN for the AJAX call, THEN wait for whatever JavaScript needs to run afterwards.

Appending a query string to your JavaScript include is a good method. Don't hate on it for no good reason, or if there is a good reason, at least explain why it's no good.
#18

[eluser]Bernat Bonet[/eluser]
Thank you waldmeister, this way is very simple to do this.
If anyone want to show detailed code for doing this way, just tell response here, and I'll post it.
#19

[eluser]ivarsmas[/eluser]
Thanks a lot to all for your answers!!

Finally i have find a solution for my problem, i will do a similar controler have proposed @waldmeister, but with some necessary change for me.

This is the controler:

Code:
class Js extends Controller
{
    function Js()
    {
        parent::Controller();
    }
  
    function get($id = 0)
    {        
        if($this->uri->segment(3))
        {
            $id = str_replace(".js", "", $this->uri->segment(3));
            $this->load->model('Mitems');
            $data = array();
            $data['test'] = $this->Mitems->get_items($id);
            $this->output->set_header("Content-type: text/javascript");
            $this->load->view('vjs', $data);
        }
    }
}

And the view is a javascript code with variables how here would be test.

Code:
(function(){
.
.
.
    var id = &lt;?=$test;?&gt;;
.
.
.
#20

[eluser]David Johansson[/eluser]
why are you taking an argument id = 0 in the get function when you don't use it, or rather why are you using the uri->segment when you have an argument 'id'?




Theme © iAndrew 2016 - Forum software by © MyBB