我应该使用PHP将CSS和JavaScript文件导入我的页面吗?

时间:2023-01-15 09:27:12

Let's suppose we have two files style.css and code.js.

假设我们有两个文件样式。css和code.js。

How should I import them into my html page? Which is the best way?

如何将它们导入到html页面?哪一种是最好的方法?

Regular

常规的

<html>
<head>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="code.js"></script>
</head>
</html>

PHP way

PHP的方式

<html>
<head>
    <style><?php include_once "style.css"; ?></style>
    <script><?php include_once "code.js"; ?></script>
</head>
</html>

Which one is the most useful way of usage?

哪一种是最有用的用法?

1 个解决方案

#1


6  

The second way is a total mess, that will load the entirety of your CSS and JavaScript on each and every page load. If you want to blow up your bandwidth bill, this is how you do it.

第二种方法完全是一团糟,它会在每个页面加载时加载整个CSS和JavaScript。如果你想让你的带宽账单变大,你可以这样做。

The first way is how it should be done. This makes the content more easily cached, it means you can serve compressed assets without a lot of server overhead. It means you can dump those assets on a Content Distribution Network (CDN) and forget about them.

第一种方法是如何去做。这使得内容更容易缓存,这意味着您可以在不需要大量服务器开销的情况下提供压缩资产。这意味着您可以将这些资产转储到内容分发网络(CDN)并忘记它们。

include_once is for PHP code, and only PHP code. Don't abuse it like this.

include_once是用于PHP代码的,并且仅用于PHP代码。不要这样滥用它。

#1


6  

The second way is a total mess, that will load the entirety of your CSS and JavaScript on each and every page load. If you want to blow up your bandwidth bill, this is how you do it.

第二种方法完全是一团糟,它会在每个页面加载时加载整个CSS和JavaScript。如果你想让你的带宽账单变大,你可以这样做。

The first way is how it should be done. This makes the content more easily cached, it means you can serve compressed assets without a lot of server overhead. It means you can dump those assets on a Content Distribution Network (CDN) and forget about them.

第一种方法是如何去做。这使得内容更容易缓存,这意味着您可以在不需要大量服务器开销的情况下提供压缩资产。这意味着您可以将这些资产转储到内容分发网络(CDN)并忘记它们。

include_once is for PHP code, and only PHP code. Don't abuse it like this.

include_once是用于PHP代码的,并且仅用于PHP代码。不要这样滥用它。