Welcome Guest, Not a member yet? Register   Sign In
how to include javascript file in codeigniter
#1

[eluser]mehwish[/eluser]
hello please anyone guide i am working in php/codeigniter and i have to incliude a javascript file like i have a view (html file) and i have written inside head tags the following:

Code:
<sscript language="javascript" type="text/javascript" src="http://localhost/fyp/js.js">                  </sscript>

and i have placed a .js file in root directory where we can see application, system , userguide folders etc. but that .js is not working or am i missing something?
#2

[eluser]Mavrick[/eluser]
Hi,

One way to do it is to create a new view and put your jscript in that view. And then load it through the controller where ever you want.

Code:
$this->load->view('jscript');
#3

[eluser]jblack199[/eluser]
&lt; script src="http://localhost/fyp/js.js" type="text/javascript"&gt;&lt;/ script> should theoretically work, unless of course you are using .htaccess to remove index.php, in which case you need to have a rewrite condition to allow access to the fyp folder.
#4

[eluser]bretticus[/eluser]
Make sure you are using an .htaccess file similar to the one found in the wiki. It has 2 rewrite conditions that will let real files override the URL rewrite when you are hiding index.php:

Code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Also, there is no reason to include a JS file via PHP whatsoever. All your javascript "lives" in the View partition of your application (I know there are reasons for dynamically rendered javascript every now and then, but you need a really good reason to do that in my opinion.)

Make sure that you are using the right path also. You do not need to use http://localhost/path_to.js you can get the same effect using /path_to.js. That way your views are portable.
#5

[eluser]mehwish[/eluser]
all is set but then also not working
#6

[eluser]jblack199[/eluser]
here is how i have mine...

Code:
&lt; script type="text/javascript" src="&lt;?php echo base_url(); ?&gt;inc/js/jquery.js"&gt;[removed]
&lt; script type="text/javascript" src="&lt;?php echo base_url(); ?&gt;inc/js/ui/ui.core.js"&gt;[removed]
&lt; script type="text/javascript" src="&lt;?php echo base_url(); ?&gt;inc/js/custom.js"&gt;[removed]

I run the .htaccess that removes index.php... and I have base_url set to:

Code:
$config['base_url']    = 'http://mydomain.com/';

take from it what you will.
#7

[eluser]bretticus[/eluser]
[quote author="mehwish" date="1313679609"]all is set but then also not working[/quote]

Do yourself a favor and get firebug in firefox immediately. That way, you can at least confirm that your javascript file is getting loaded (you can do the same thing in IE8/9 with built-in developer tools or Chrome Developer Tools. Firebug has the most bells and whistles though.)
#8

[eluser]ted wong[/eluser]
I just type it manually.




Theme © iAndrew 2016 - Forum software by © MyBB