简体   繁体   中英

How to add and use an external js file in Codeigniter?

I am trying to add an external javascript file to Codeigniter application.

I have placed my external js file in the "projectname/js/" folder.

Below is the script tag src in my view page. src="http://localhost/needpcbcodeigniter/js/registration.js">

But I am getting a "Not found" error for the js file.

Most probably this is because the apache mod_rewrite is dealing with the js folder as a codeigniter controller name (as if you're calling http://localhost/projectname/index.php/js/registration.js )

Check your .htaccess to make sure it doesn't redirect when a valid filename is requested. Check it here

You should add an external javascript file to codeigniter in assets folder.

You should check some following steps :-

1) first fix your project base_url which is set in application/config/config.php like-

$config['base_url'] = 'http://localhost/yourprojectname/'

2) create a folder(eg: js) in assets folder(at root of folder) for save all javascript files(eg : registration.js).

3) on final step where you want to use javascript file use code like this-

<script src="<?php echo base_url();?>/assets/js/registration.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM