1

is there a way to load css and/or javascript files from outside of the public web directory?

for example on my hosting service i have /public_html but don't want these files to exist in the public directory and want them in a directory outside of the public directory in a sibling directory /system (i am using codeigniter) within the /system/application/view/

2
  • 2
    That does not make any sense. You want to serve the content of those files, so put them in the public_html folder.. Commented May 28, 2010 at 1:54
  • i know i want to serve them up, what i meant was there a way to process these files from some source outside of public. it's annoying having a view layer that exists as css/js in /public while having your view templates outside of public in some other directory. so for the sake of maintaining all view components i would like these files to all exist in the same directory and not all in the public_html. the question however does make sense. Commented May 28, 2010 at 2:21

5 Answers 5

4

Ultimately, Javascript and Stylesheets are processed on the client side. For that reason, there is no solution that would truly hide your javascript or CSS from the public.

One possible solution is to load the required CSS/ Javascript file via PHP using something like file_get_contents() and then outputting that directly to the page using inline styles / scripts.

This doesn't really solve your problem of hiding the code / styles from the public though. It would give you the option of filtering all code and styles through some kind of packer or obfuscatory, although there's no reason you couldn't do that with your static files (and at much less of a processing expense)

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

Comments

1

Yes -- in a way -- and Minify [http://code.google.com/p/minify/] is one approach.

Look at line 39 of the config file [http://code.google.com/p/minify/source/browse/trunk/min/config.php]. Here you will see where your minified cache sits outside of the web root. Now, I do not know if the source JS and CSS can sit in the same directory as the cache.

1 Comment

this is precisely what i'm looking for. i had a brain fart while typing up my question but you nailed it. thanks!
1

Not without a public facing proxy.

You will need to file_get_contents() or include them and then serve them to your page.

You can not just do ../../system and get above the DOCROOT.

They need to be processed by the browser, so they need to be accessible.

If you want to hinder people viewing your source in a human readable way, check out CSS minify and JS packer. These of course are only obfuscating the code. Anyone determined will be able to read your JavaScript and see what it does.

Why don't you want people to read your CSS or JavaScript?

1 Comment

never said i didn't want them to read the files, that's not a concern. i just want to maintain all view and public files in the same directory, opposed to splitting view templates and javascript/css within codeigniter's file structure.
0

I know what you mean twmulloy, it seems inconsistent to have 'view' related information in different places. However, consider that the JS and CSS files are resources that support the views, rather than parts of the view themselves.

That said, you can achieve what you want in a number of ways. One might be to write a controller that accepts requests for your JS/CSS assets and outputs a header and data from the relevant place (a view file, the database, anywhere in fact). However, this is inefficienty compared to just accepting the 'untidiness' of popping the files in a subfolder of the root level public_html. I, like many commentors above, feel this is the best solution for its speed and appropriateness; just having an 'assets' directory at the same level as the 'system' one, with images, css, js etc inside. You could use an alias or virtual folder to make things feel better for you...

However, there is a third way. There are libraries that do something JUST like what you want, with the added benefit of Minify (from the accepted answer) and compression, or whatever you fancy. The two libraries I know of are called AssetLibPro and Carabiner, and these allow you to specify an asset path (as you want), and then you load your JS and CSS files (with groups e.g. screen, print if needed). They then serve up all related CSS/JS etc as one file; compressed, minified, cached... whatever you need.

Comments

0

I know of one situation where this is necessary. If I have several versions of files and I don't want all versions in all instances, it would be ideal to put these versions in some hidden folder and load only the version I want. Something like a CDN, but on its own server.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.