全栈JavaScript之路(十七)HTML5 新增字符集属性

HTML5 添加�了几个文档字符集属性。

document.charset : 表示文档的实际使用的字符集。

document.defaultCharset: 表示默认的字符集,跟浏览器以及操作系统设置有关。


假设文档没有使用默认字符集,那么 document.charset 与 document.defaultCharset 可能不同。


设置文档字符集: 但能够通过<meta>元素、响应头部或直接设置charset 属性改动这个值。来看三个样例。


  • <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  • document.charset = "utf8";
  • res.header(200,{'Content-Type': 'text/html; charset=UTF-8'}) //node.js 设置响应头


支持document.charset 属性的浏览器有IE 、Firefox 、Safari 、Opera 和Chrome 。

支持document.defaultCharset 属性的浏览器有IE、Safari 和Chrome。

原文地址:https://www.cnblogs.com/hrhguanli/p/4085738.html