简体   繁体   English

PHP - 包括而不是<script>

[英]PHP - include instead of <script>

between this 这之间

<script src="js/script.js"></script>

and that 然后

<?php
    echo '<script>';
    include 'js/script.js';
    echo '</script>';
?>

Which is better? 哪个更好?
I'm actually wondering about things like HTTP Request and others stuffs... 我实际上想知道HTTP请求和其他东西......

(the same goes for CSS styles, should I put everything in the same file and send to the user, thus reducing the amount of requests, or should I properly separate just like everyone else do? thus increasing the number of requests) (同样适用于CSS样式,我应该将所有内容放在同一个文件中并发送给用户,从而减少请求数量,还是应该像其他人一样正确分离?从而增加请求数量)

There is something else that I should be concerned about? 还有什么我应该关注的吗?

Ok, it took me second to figure out what you were asking. 好吧,我花了第二时间弄清楚你在问什么。 In your first choice you are outputing a script tag that links to your javascript, in the second you using PHP to include your javascript inline. 在您的第一个选择中,您输出的是一个链接到您的javascript的脚本标记,在第二个选择中您使用PHP来包含您的javascript内联。

Of the two choices, the first is by far the best . 在这两个选择中, 第一个是迄今为止最好的选择 Assuming your page content is dynamic, due to browser caching, for every page a person downloads from you, the same javascript will be included everytime. 假设您的页面内容是动态的,由于浏览器缓存,对于每个人从您下载的页面,每次都会包含相同的JavaScript。 If your javascript is 100kb in size, every page is now an extra 100kb. 如果您的javascript大小为100kb,那么每个页面现在额外增加100kb。 Over time this will add up for both your server and your clients. 随着时间的推移,这将增加您的服务器和客户端。

Including your Javascript (and CSS) by linkages allows the browser to cache pages, and only fetch what is necessary. 通过链接包括您的Javascript(和CSS)允许浏览器缓存页面,并且只获取必要的内容。 This will similarly reduce the number of requests as a browser will only fetch what is necessary, which in most cases is just the HTML page. 这同样会减少请求的数量,因为浏览器只会获取必要的内容,这在大多数情况下只是HTML页面。

edit: What if the script is used on only one page? 编辑: 如果脚本仅在一个页面上使用怎么办?

Still include the Javascript by a link, rather than inline. 仍然通过链接包含Javascript,而不是内联。 If you page is 100% static, but has thats not one page but many. 如果您的页面是100%静态的,但不是一页而是多页。 And each request will get a new output, with the same replicated Javascript. 并且每个请求都将获得一个新的输出,具有相同的复制Javascript。 Even if your page is pure-static HTML, still include it by a link as you never know when you might want to reuse the Javascript (or CSS) code. 即使您的页面是纯静态HTML,仍然通过链接包含它,因为您不知道何时可能需要重用Javascript(或CSS)代码。

I would consider something like this 我会考虑这样的事情

<script src="js/js.php"></script>

where js.php includes all the need js files I assume this will resolve the caching issue, plus you can make things dynamic by adding get values I guess. 其中js.php包含了所有需要的js文件,我认为这将解决缓存问题,而且你可以通过添加获取值来使事物变得动态。

btw I find it better to use the php open and close tags for html whenever possible 顺便说一下,我发现尽可能使用PHP的open和close标签更好

   <script src="<?php echo $var ?>" ></script>

As I commented before, <script src="js/script.js"></script> . 正如我之前评论过的, <script src="js/script.js"></script>

This is in you <head> and it will be implemented before anything goes into you <body> 这是在你的<head> ,它将在任何进入你<body>之前实现

Since you are building your front end via JavaScript, php functionality will come after everything was built by JS. 由于您是通过JavaScript构建前端,因此在JS构建所有内容之后将会出现PHP功能。

Well according to me using the later approach is better , if you are designing a php page it is always better to write everything in php , whereas HTML side scripting is better done inside echo"" or print""; 好吧,根据我使用后面的方法更好,如果你正在设计一个PHP页面,总是更好地在PHP中编写所有内容,而HTML侧脚本最好在echo“”或print“”内完成; functions , you can read a lot about it in w3schools.com , hope my answer solved your problem. 功能,你可以在w3schools.com上阅读很多相关内容,希望我的回答能解决你的问题。

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

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