简体   繁体   中英

How to inlucde external javascript libraries in Codeigniter

I have a web page which needs to include external javascript libraries. I have downloaded them and include in the php->libraries folder. My application is hosted in openshift cloud. I want to include those javascript files in my php file. I have used this code

<script src=”../../libraries/dhtmlxCalendar/dhtmlxCalendar/codebase/dhtmlxcalendar.js”></script>

But it is not working

Then I tried

<script type=“text/javascript” src=”<?php echo base_url();?>js/jquery.js” ></script>

The base url gives me the url but it is also not working.

What is the correct way for this ? I am a beginner for codeigniter. Could you help me

The way I would do it is:

<script type="text/javascript" src="<?php echo base_url('js/jquery.js'); ?>"></script>

This should output:

<script type="text/javascript" src="http://www.yourdomain.com/js/jquery.js"></script>

If the above still doesn't work, chances are the file cannot be found and it's in a different directory.

You could also use an asset helper to added script files.

  1. Create a folder called 'assets' in your root CodeIgniter folder. Your root folder must now have these 4 folders: applications, assets, system, user_guide.

  2. Create a folder called js and put your Javascript file there.

  3. Then, open the config.php file located at \\application\\config\\config.php.

  4. Set the base url to your root folder. You can replace www.yoursite.com by localhost if your developing locally. /folder is the name of your root folder

    $config['base_url'] = ' http://www.yoursite.com/folder/index.php ';

  5. Use this script in your view:

" >
 <script type="text/javascript" src="<?php echo base_url().'..assets/js/javascriptfile.js'; ?>" ></script> 

Just replace javascriptfile with the your js filename.

This is how I do it and it will surely work.

Are you using a .htaccess file where you rewrite the access rules?

If so you must include the .js extension in that file (see below):

# Allow these directories and files to be displayed directly:
RewriteCond $1 ^(index\.php|txt|js|robots\.txt|images|css|opensearch\.xml|favicon\.ico|assets|forums)

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