douyin2883 2017-06-02 13:25 采纳率: 0%
浏览 82
已采纳

使用PHP在HTML中回显css和js的内容

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

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


<script>
 <?php echo require_once("js/myjs.js"); ?>
</script>
  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀