zoukankan      html  css  js  c++  java
  • 获取当前文档编码的思考

    最近有需要想自动获取当前文档编码,遇到了现在的问题:

    之前想通过document.charset获取,遇到了各种坑~ 就查询了一下一些获取文档编码的方式

    第一种:

    document.charset

    设置或检索使用的字符集编码的对象

    A LINK SCRIPT可以设置charset属性值,只有设置了才可以获取该属性值

    第二种:

    document.defaultCharset

    获取当前区域语言设置的默认字符编码

    以上两种写法 Gecko不支持

    第三种:

    document.characterSet

    该字符编码是用来渲染当前网页所使用的字符集,它的值不一定是当前页面的正确的字符编码(因为用户可以选择使用其他的编码来渲染当前页面)

    总结:

    document.charset是指认为设置的编码

    document.defaultCharset是指地区默认编码

    document.characterSet最终文档渲染的编码

    备注:

    <!DOCTYPE HTML>
    <html >
    <head>
    <meta charset="utf-8">
    <!--meta content="text/html;charset=gbk"-->
    <script>
    alert("document.charset=>"+document.charset+"
    document.defaultCharset=>"+document.defaultCharset+"
    document.characterSet=>"+document.characterSet);
    </script>
    <head>
    <body>
    </body>
    </html>

    各浏览器执行情况:

     

    Chrome

    FF

    Opera

    Safari

    IE/6

    document.charset

    UTF-8

    undefined

    UTF-8

    UTF-8

    utf-8/utf-8

    document.defaultCharset

    GBK

    undefined

    GBK

    ISO-8859-1

    gb2312/gb2312

    document.characterSet

    UTF-8

    UTF-8

    UTF-8

    UTF-8

    utf-8/undefined

    所以获取当前文档编码的方式,可以这样实现:

    //实现获取当前页面编码 如果是UTF-8的就直接显示,如果不是就默认成GBK
    var fileCharset = document.charset ? document.charset : (document.characterSet ? document.characterSet : document.defaultCharset);
    var utf8Set = "utf-8|UTF-8";
    if(utf8Set.indexOf(fileCharset)>-1){
        charset = fileCharset;
    }else{
        charset = "GBK";
    }
    alert(charset);

    参考:https://developer.mozilla.org/zh-CN/docs/DOM/document.characterSet

      

  • 相关阅读:
    Deformable 可变形的DETR
    https://start.aliyun.com/
    english note 111
    HTTP/2.0与HTTP/1.1协议区别
    什么是长连接
    使用pycharm
    Java使用率大幅度下降,Python使用率逐渐攀升
    SELECT command denied to user 'root'@'localhost' for table 'user'
    mysql 问题阅后归档
    响应式编程
  • 原文地址:https://www.cnblogs.com/xiaoheimiaoer/p/3296550.html
Copyright © 2011-2022 走看看