html源代码中空格的值
我正在使用一个函数来生成 php 中的所有输出。使用该函数,我可以控制是否显示这样的代码:
<html><header></header><body><p>Hello World!</p></body></html>
或像这样
<html>
<header>
</header>
<body>
<p>Hello World!</p>
</body>
</html>
包括缩进和所有内容。
显示缩进和间隔的代码是否有特定的价值(除了看似较慢的加载时间)?我通常不需要查看源代码,因为我可以简单地访问 PHP 文件。在开发过程中,我很可能更喜欢空白,但在生产过程中,它一定是有利的吗?
谢谢!
I'm using a function to generate all output in php. Using that function I can control whether to display the code like this:
<html><header></header><body><p>Hello World!</p></body></html>
or like this
<html>
<header>
</header>
<body>
<p>Hello World!</p>
</body>
</html>
including the indentation and all.
Is there a particular value to displaying the code indented and spaced (besides seemingly slower loading time)? I usually don't need to view the source code, since I can simply access the PHP file. During development I would most likely prefer whitespace, but when on production would it necessarily be advantageous?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您可以选择的话,我会将其间隔开,使用空白来使某些内容可读并没有什么问题,而且使用GZip,它使下载差异不再那么重要。你永远不知道什么时候需要调试样式,现在就拥有它会节省你的时间,相信我。
I'd space it out if you have the option, there's nothing wrong with white-space to make something readable, and with GZip it makes the download difference not all that major anyway. You never know when you'll have to debug a style, it'll save you time later by having it pretty now, trust me.
所有空白都被压缩为一个空格,而不是空无一物,因此存在细微差别。例如:
会产生与此略有不同的结果:
因此,至少在标签之间使用单个空格/换行符。就我个人而言,我更喜欢在实时站点上使用间距,因为它有助于实时调试,并且在使用 gzip 时,空格和无空格之间的差异无论如何都是很小的。
当然,它也会帮助那些可能想看看“它是如何完成的”的新开发者。
All whitespace is condensed to a single space, rather than nothing, so there is a slight difference. For example:
Will produce slightly different results to this:
So at a minimum, use a single space/newline between tags. Personally I prefer using spacing on live sites because it aids live debugging, and when using gzip the difference between space and no-space is tiny anyway.
And of course, it would also help budding new developers who might like to see "how it was done".
我更喜欢省略空格,尤其是在生产中。
您仍然可以通过 Firebug 查看代码。没有理由“查看源代码”。
请注意,空格可能会导致一些问题,因为它们被视为空格。
I prefer to omit whitespaces, especially in production.
You can still view the code through Firebug. there is no reason to do "view source".
Note that spaces can cause some problems, because they are considered as a space.
如果你删除了空格,有一天当你必须在某个远程客户端计算机上的 Internet Explorer 中查看源代码并且必须费力地浏览一大堆 HTML 标签时,你就会后悔自己的节俭本性。
If you strip out whitespace you'll rue your parsimonious nature one day when you have to View Source in Internet Explorer on some remote client machine and have to wade through a swamp of HTML tags.
空白会不必要地积累网络带宽。不,GZIP 不会 100% 修复它。我自己删除了响应中的所有空白,然后通过 GZIP 传递它。唯一关心 HTML 源代码中的空格的是那些对页面源代码的外观感到好奇的 Web 开发人员。它们确实不值得浪费网络带宽——除非您实际上是唯一的访客;)
Whitespace will unnecessarily accumulate network bandwidth. No, GZIP won't fix it up to with 100%. I myself trim all the whitespace from the response and then pass it through GZIP. The only ones who cares about whitespace in HTML source are webdevelopers who are curious how the page source look like. They are really not worth the waste of network bandwidth --unless you're practically the only visitor ;)
您的 Html 是由 Javascript 还是 PHP 生成的?无论哪种方式 - 使用转义字符。
\n = 换行
\t = 制表符
\r = 回车
Is Javascript or PHP generating your Html? Either way - utilize an escape character.
\n = new line
\t = tab
\r = carriage return