CodeIgniter Forums
Javascript broken paths issue - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Javascript broken paths issue (/thread-33250.html)



Javascript broken paths issue - El Forum - 08-19-2010

[eluser]fiktionvt[/eluser]
I have started a new CI project and I am have trouble with the paths to my images / javascript /css folders. My directory structure is like so

ci/public/images/..
ci/public/css/..
ci/public/js/..
ci/application/..

I have broken up the header, footer into two views so when I call my index() function when the page loads it looks something like this

$this->view->load('header');
$this->view->load('main');
$this->view->load('footer');

In my routes.php I point my site to load site.php controller in the application/controllers folder so when the page loads it calls my index function and everything works but the url of my page is shown as www.example.com,

When I navigate away by clicking a link to a different page located at www.example.com/ci/sites/contact
all of my links to my javascript / css / images in my header view are not found because the path is looking off of what is in my url which is www.example.com and not www.example.com/ci/site/contact, how can I make it always point to the same location?


Javascript broken paths issue - El Forum - 08-20-2010

[eluser]Clifford James[/eluser]
What does your header look like?

You can use this in your header:
Code:
<link rel="stylesheet" type="text/css" href="<?=site_url('assets/stylesheets')?>/style.css"/>



Javascript broken paths issue - El Forum - 08-20-2010

[eluser]mddd[/eluser]
You need to realize that the browser treats a CI application just like any other website. If your page is /controller/method/argument and you link to your js-files as "js/script.js" then the browser is going to look for that file in /controller/method/js/script.js. And that doesn't exist. You need to explicitly tell the browser where the file is, and not use a relative path because that will look different to the browser depending on how your url looks for each page.

One way to do this is as Clifford James points out: use the site_url of base_url functions. They will put in your site's url from config.php and make a consistent location. You could also just use absolute urls from the root of your site, like "/js/script.js". Using the functions has the advantage of easier moving around of the entire site if you want to do so in the future.