2017-06-02 13:25
浏览 82


To reduce http requests, is it okay to inline css and js using php ? Eg:

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

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

图片转代码服务由CSDN问答提供 功能建议

要减少http请求,是否可以使用php内联css和js? Eg: \ n

 &lt; style&gt; 
&lt;?php echo require_once(“css / style.css”);  ?&gt; 
&lt; / style&gt; 
&lt; script&gt; 
&lt;?php echo require_once(“js / myjs.js”);  ?&gt; 
&lt; / script&gt; 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

  • doucan8246326 2017-06-02 13:50

    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. There are certain cases where it's a good idea to add your CSS and JS into an internal stylesheet inside your document.

    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:

    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.

    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. 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. If you truly want to reduce your http requests then you should seriously consider merging your stylesheets and script files. It's better to have a few large files than a lot of tiny ones.

    解决 无用
    打赏 举报

相关推荐 更多相似问题