简体   繁体   English

使用PHP在HTML中回显CSS和JS的内容

[英]Echo content of css and js inside HTML using PHP

To reduce http requests, is it okay to inline css and js using php ? 为了减少http请求,可以使用php内联css和js吗? Eg: 例如:

<style>
 <?php echo require_once("css/style.css"); ?>
</style>


<script>
 <?php echo require_once("js/myjs.js"); ?>
</script>

I'm going to answer the second half of your question first, and then apply the first half. 我将先回答您问题的后半部分,然后再应用前半部分。

" is it okay to inline css and js using php " - yes. 可以使用php内联CSS和JS吗? ”-是的。 There are certain cases where it's a good idea to add your CSS and JS into an internal stylesheet inside your document. 在某些情况下,最好将CSS和JS添加到文档内部的样式表中。

Your method however could do with a lot of improvement. 但是,您的方法可以做很多改进。 A quick way to do this would be to use file_get_contents like this: 一种快速的方法是使用file_get_contents这样的:

echo '<style type="text/css">'.file_get_contents('/path-to-your/style.css').'</style>';

The only cases where I would suggest grabbing file contents and adding them into the document directly are cases that involve writing plugins for CMS systems such as Wordpress or Joomla etc. 我建议捕获文件内容并将其直接添加到文档中的唯一情况是涉及为CMS系统(如Wordpress或Joomla等)编写插件的情况。

Sometimes, your plugin may only need to utilise 5 - 10 lines of CSS and/or JS , and it would be bad form to force your user to add another stylesheet to their http requests for so little code. 有时,您的插件可能只需要使用5至10行CSS和/或JS ,这是一种不好的形式,迫使您的用户只需很少的代码就可以向其http请求添加另一个样式表。 In these cases, I would suggest loading internal stylesheets/scripts. 在这些情况下,我建议加载内部样式表/脚本。

BUT it's not a good idea to do this to " To reduce http requests " as your first half of the question has asked. 但是 ,按照问题的前半部分要求减少HTTP请求 ”不是一个好主意。 If you truly want to reduce your http requests then you should seriously consider merging your stylesheets and script files. 如果您确实想减少http请求,那么您应该认真考虑合并样式表和脚本文件。 It's better to have a few large files than a lot of tiny ones. 最好有几个大文件,而不是很多小文件。

This looks like a really bad practice to me. 对我来说,这似乎是一种非常糟糕的做法。 Plus, I don't see how you will have any performance gain here, more like the contrary. 另外,我看不到您如何在此处获得任何性能提升,更像是相反。

You should more focus on WHEN do you really need to load your js and css. 您应该更加关注何时真正需要加载js和CSS。 Minifying it should help too. 缩小它也应该有所帮助。

Printing resources like JS or CSS into file to load them faster is definitely bad practice to me. 对我来说,将JS或CSS之类的资源打印到文件中以更快地加载它们绝对是不好的做法。 Modern browsers eventually cache them while user hit your first page. 当用户点击您的第一页时,现代浏览器最终将它们缓存。

According to me you can try to minify your resources files to load them faster and improve caching if you want but there is no need of it because all modern browsers handle caching pretty good now. 根据我的说法,您可以尝试缩小资源文件的速度,以便更快地加载它们并根据需要改进缓存,但是由于所有现代浏览器现在都可以很好地处理缓存,因此不需要它。

Here is what I do to load CSS as fast as possible and take advantage of browser cache at the same time: 这是我尽可能快地加载CSS并同时利用浏览器缓存的方法:

I check or set a cookie for first-time visitors; 我为首次访问者检查或设置cookie; if if it's a first-time visitor I load CSS inline + prefetch the actual link (note that I use prefetch as it is widely supported by modern browsers as opposed to preload). 如果是初次访问者,则我加载CSS内联+预取实际链接(请注意,我使用预取,因为现代浏览器广泛支持它而不是预载)。 That way this visitor will get the prefetched CSS from cache on the next visit or the next page/s. 这样,此访问者将在下次访问或下一页从缓存中获取预提取的CSS。

if(isset($_COOKIE['v'])) $CSS='<link href="default.css" rel="stylesheet">';
else {
$CSS='<style>'.file_get_contents('default.css').'</style><link rel="prefetch" href="default.css">';setcookie('v','1',time()+31556926,'/','example.com',1,1);
}

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

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