dreamend
dreamend

Reputation: 79

Using JavaScript with Codeigniter

I have a CI folder structure like this:

-> root  
- - >application  
- - >resources  
- - >system

In the resources folder, I have separate folders like 'css', 'images', 'js', 'js/plugins' etc.

I have a twitter plugin in js/plugins folder which needs the base path of the site. I used it like this :

modpath: 'resources/plugins/twitter/',  

And it works if the url that i visited is localhost/myapp/ , but doesn't work when the url is localhost/myapp/index.php/welcome

What i have to do ?

Because it is not a controller or a view, i can't use it as <?=site_url();?> right? So what i need to do?

Thanks for help !

Upvotes: 0

Views: 1168

Answers (6)

Wide Vision
Wide Vision

Reputation: 15

Create js folder on the root where application and system folders are placed then place javascript file on that folder then on the view where you want to use this javascript file write down the following path:-

<script src="<?php echo $this->config->item('base_url') ?>js/fileName.js"></script>

Upvotes: 0

ahmad
ahmad

Reputation: 2729

In your view file, before loading any javascripts add something like this

<script>
var base_url = '<?php echo base_url(); ?>';
</script>

This would make a javascript variable called base_url available to all other files you load after the definition.

Now in your javascript file you can do something like:

modpath: base_url + 'resources/plugins/twitter/',

Upvotes: 1

oentoro
oentoro

Reputation: 750

I usually do this, in my View:

<script src="<?php echo base_url();?>resources/js/yourscript.js"></script>
<link rel="stylesheet" href="<?php echo base_url();?>"resources/css/bootstrap.css" />

to load resources (script files, or anything else) that stored locally.

Upvotes: 0

sanjeeviraj
sanjeeviraj

Reputation: 155

Check this with ur view file

<script src="<?php echo base_url();?>resources/plugins/twitter/twitter.js"></script>

Upvotes: 0

Tahir Jamil
Tahir Jamil

Reputation: 21

Assume your base_url() is localhost/myapp/

Then use $this->config->base_url() in src like

<script src="<?= $this->config->base_url(); ?>resources/plugins/twitter/twitter.js"></script>

Upvotes: 0

Lucas Ferreira
Lucas Ferreira

Reputation: 888

You should use the full path to your files, in my CI projects I use something like this:

<script src="<?=base_url()?>js/twitter.js

Remember that your assets must be in the /public folder.

Upvotes: 1

Related Questions