CodeIgniter Forums
javascript function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: javascript function (/showthread.php?tid=28102)



javascript function - El Forum - 03-02-2010

[eluser]husni[/eluser]
this is my js file name example.js
Code:
function hello()
{
       alert('Hello hehe');    
}

this is my controller
Code:
<?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();    
    }
    
    function index()
    {$this->load->view('welcome_message');}
}

this is my view file
Code:
<html>
<head>
<title>Welcome to CodeIgniter</title>
<XXscript type='text/javascript' src='&lt;?php base_url();?&gt;javascript/example.js'></XXscript>
&lt;/head&gt;
&lt;body&gt;
something..something..
&lt;/body&gt;
&lt;/html&gt;

i want to call the hello() function in my view file..anyone can help me?


javascript function - El Forum - 03-02-2010

[eluser]farinspace[/eluser]
then call it ...

either call the function in example.js (which would call it on every page that uses the example.js file) or simply include an inline script and call the function for the specific view ...

Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Welcome to CodeIgniter&lt;/title&gt;
<XXscript type='text/javascript' src='&lt;?php base_url();?&gt;javascript/example.js'></XXscript>
&lt;/head&gt;
&lt;body&gt;
something..something..

<XXscript>
hello();
</XXscript>

&lt;/body&gt;
&lt;/html&gt;



javascript function - El Forum - 03-02-2010

[eluser]husni[/eluser]
[quote author="farinspace" date="1267532949"]then call it ...

either call the function in example.js (which would call it on every page that uses the example.js file) or simply include an inline script and call the function for the specific view ...

Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Welcome to CodeIgniter&lt;/title&gt;
<XXscript type='text/javascript' src='&lt;?php base_url();?&gt;javascript/example.js'></XXscript>
&lt;/head&gt;
&lt;body&gt;
something..something..

<XXscript>
hello();
</XXscript>

&lt;/body&gt;
&lt;/html&gt;
[/quote]

didn't works..


javascript function - El Forum - 03-02-2010

[eluser]maria clara[/eluser]
You can place your images etc within separate folders too if you wish. The folders must be in the same directory as your index.php file though.

You should not be putting any static content anywhere in your application folder. I tend to setup my CI directory structure like so

Quote:My CodeIgniter App
|
+-- System/
|
+-- Application/
|
|
+-- lib/ (libraries used is stored here and that are called in the header also)
| |
| +-- images/
| |
| +-- js/
| |
| +- style.css
|
+- index.php

When loading say .js within my views for example I'd use
Code:
<XXscript type="text/javascript" src="&lt;?=echo base_url()?&gt;lib/js/js file">  </XXscript>

then in ur controller:

Code:
function js()
{
$this->load->view('js/js file.js');
}



javascript function - El Forum - 03-02-2010

[eluser]farinspace[/eluser]
Make sure that the browser is loading the "example.js" file properly ...