简体   繁体   English

从非公共目录加载CSS或JavaScript

[英]loading css or javascript from non-public directory

is there a way to load css and/or javascript files from outside of the public web directory? 有没有办法从公共Web目录外部加载CSS和/或JavaScript文件?

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/ 例如,在我的主机服务上,我有/ public_html,但不希望这些文件存在于公共目录中,并且不希望它们位于/ system中同级目录/ system(我正在使用codeigniter)的公共目录之外的目录中/应用程序/视图/

Ultimately, Javascript and Stylesheets are processed on the client side. 最终,JavaScript和样式表在客户端进行处理。 For that reason, there is no solution that would truly hide your javascript or CSS from the public. 因此,没有解决方案能够真正向公众隐藏您的JavaScript或CSS。

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. 一种可能的解决方案是使用诸如file_get_contents()之类的东西通过PHP加载所需的CSS / Javascript文件,然后使用内联样式/脚本将其直接输出到页面。

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) 它使您可以选择通过某种打包程序或混淆处理来过滤所有代码和样式,尽管没有理由您无法使用静态文件来做到这一点(而且处理费用要少得多)

Yes -- in a way -- and Minify [ http://code.google.com/p/minify/] is one approach. 是的-从某种意义上讲- 缩小 [ http://code.google.com/p/minify/]是一种方法。

Look at line 39 of the config file [ http://code.google.com/p/minify/source/browse/trunk/min/config.php] . 查看配置文件[ http://code.google.com/p/minify/source/browse/trunk/min/config.php]的第39行。 Here you will see where your minified cache sits outside of the web root. 在这里,您将看到缩小的缓存位于Web根目录之外的位置。 Now, I do not know if the source JS and CSS can sit in the same directory as the cache. 现在,我不知道源JS和CSS是否可以与缓存位于同一目录中。

Not without a public facing proxy. 并非没有面向公众的代理人。

You will need to file_get_contents() or include them and then serve them to your page. 您将需要file_get_contents()include它们,然后将它们提供给您的页面。

You can not just do ../../system and get above the DOCROOT. 您不仅可以执行../../system并超越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 . 如果您想阻止人们以易于理解的方式查看您的源代码,请查看CSS minifyJS packer These of course are only obfuscating the code. 当然,这些只是混淆了代码。 Anyone determined will be able to read your JavaScript and see what it does. 确定的任何人都将能够阅读您的JavaScript并查看其功能。

Why don't you want people to read your CSS or JavaScript? 您为什么不希望人们阅读您的CSS或JavaScript?

I know what you mean twmulloy, it seems inconsistent to have 'view' related information in different places. 我知道您的意思是twmulloy,在不同的地方使用“查看”相关信息似乎不一致。 However, consider that the JS and CSS files are resources that support the views, rather than parts of the view themselves. 但是,请考虑JS和CSS文件是支持视图的资源,而不是视图本身的一部分。

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). 一种可能是编写一个控制器,该控制器接受对您的JS / CSS资产的请求,并从相关位置(视图文件,数据库,实际上是任何地方)输出标头和数据。 However, this is inefficienty compared to just accepting the 'untidiness' of popping the files in a subfolder of the root level public_html. 但是,与仅接受在根级别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. 仅具有与“系统”相同级别的“资产”目录,其中包含图像,css,js等。 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. 有一些库可以像您想要的那样做一些事情,并具有Minify(来自公认的答案)和压缩的额外好处,或者您喜欢的任何东西。 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 eg screen, print if needed). 我知道的两个库分别称为AssetLibPro和Carabiner,它们允许您指定资产路径(根据需要),然后加载JS和CSS文件(包括组,例如屏幕,必要时进行打印)。 They then serve up all related CSS/JS etc as one file; 然后,它们将所有相关的CSS / JS等作为一个文件提供; compressed, minified, cached... whatever you need. 压缩,缩小,缓存...任何需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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