0

[Edited Question]

Okay, sorry for my confusing question. Actually I want to locate my image/css/js in the view folder. If I load by using <script src="application/views/script.js" />, 403 forbidden message is appeared. Recently I found the answer here https://stackoverflow.com/a/6573951/842264. But is it safe to edit the htaccess? Thank you.

2
  • Kindly tell your problem more clearly so we can asnwer it Commented Apr 24, 2012 at 6:49
  • I am sorry for my confusing question. Actually I want to load js as an external js and that js file is in the view folder. But if we located file in the view folder, we should load that file by calling $this->view->('script.js'). But if I load it by using that call, the js will be loaded as an inline js, not as an external js. I hope it is more clear than before. Commented Apr 26, 2012 at 4:30

2 Answers 2

1

Try using William's Concepts Template Library. This works with Codeigniter 2+.

You'll need to use the add_js function of Template. This is discussed here: http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html#utilities

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. So if I want to load js that located in the view folder, I can call $this->template->add_js($this->view->('script.js'), 'embed') ?
0

Well there are two simple ways . First Method

Creates a folder assets in ci main folder which should contain 3 folders css , images and scripts Now go to config/constants.php and define a constant there let say JS

define('JS','http://localhost.com/myapp/assets/scripts');

And now in your view file just write

<script type="text/javascript" src="<?php echo JS?>/myfile.js"></script>

It will be loaded. The other method is that go to config.config.php Change base url

$config['base_url'] = 'http://localhost/task/';
$config['js'] = 'assets/js';

And now in controller

$data['js'] = $this->config->item('base_url').$this->config->item('js');

And now in view

<script type="text/javascript" src="<?php echo $js?>/myfile.js"></script> 

3 Comments

Thank you for answer. But I locate my js file in the views folder, not root/main folder.
locating in view folder is not a good practice and in codeigniter you should seperate them in another folder for better arrangments. httaccess should not be changed unless it is necessary
I have located my general js in the root folder and I loaded it for every pages by using <script src="assets/js/script.js />". But this js is a specific js for specific module. Thus I locate the view module (index.php in application/views/module/index.php) and js module (script.js in application/views/module/script.js) in one folder. Different module has different folder which contains different index.php and script.js. I think it is more organized for me. But thank you for answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.