将数据包含在HTML页面中的最佳方式是什么? [英] What is the best way to include data in an HTML page?

查看:75
本文介绍了将数据包含在HTML页面中的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在HTML页面中包含数据的最佳方式是什么?这些数据不是人类可读的,并且一旦页面被加载,将由脚本处理。我能想到的选项是:




  • 在隐藏/空的上使用类和标题属性< div> < span> 页面内的元素


  • JSON在页面底部的< script> 元素中


  • 通过XMLHttpRequest在页面加载后


  • XML数据岛




所有这些方法似乎都有缺点,所以我想知道您的想法是什么。 < script> 元素中使用JSON。其实,我会把它变成一个实际的脚本。浏览器将解析和评估JSON,因此请抓住机会将其存储在某个变量中。



使用CSS隐藏元素是一种脆弱的方式,因为某些客户端不一定是浏览器,认为搜索引擎)仍然可以将它们视为页面数据的一部分。



在页面加载完成后通过XHR加载数据,但并不严格回答这个问题。它也慢一点,因为它会导致额外的服务器往返(想想你的antipodean用户,低延迟对他们来说非常重要)。

XML Data Islands:I我不确定你指的是什么,但它听起来像是会引起很多验证器投诉的东西,而且可能会很脆弱,因为可以通过浏览器呈现字符串节点。



<因此,将数据存储在< script> 元素中听起来像是回答问题最简单,最安全,最合适的方式。


What is the best way to include data in an HTML page? The data is not human-readable and will be processed by a script once the page has loaded. The options I can think of are:

  • Using class and title attributes on hidden/empty <div> or <span> elements within the page

  • JSON in a <script> element at the bottom of the page

  • Load the data via an XMLHttpRequest after the page has loaded

  • XML Data Islands

All of these methods seem to come with drawbacks so I would like to know what your thoughts are.

解决方案

I would use JSON in a <script> element. Actually, I would make it an actual script. The browser is going to parse and evaluate the JSON, so take the opportunity to store it in some variable.

Hiding elements using CSS is kind of fragile, as some clients (not necessarily browsers, think search engines) may still see them as part of the page data.

Loading the data through XHR after page load is fine, but it's not strictly an answer to the question. Its also a bit slower as it incurs an additional server round-trip (think of your antipodean users, low latency is very important to them).

XML Data Islands: I am not sure what you are referring to, but it sound like something that would cause much validator complaints, and which might be fragile in that string node could be rendered by the browser.

So, storing the data in a <script> element sounds like the simplest, safest, most appropriate way to answer the question.

这篇关于将数据包含在HTML页面中的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆