• 获取当前文档编码的思考


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

    之前想通过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

      

  • 相关阅读:
    SubVerSion 快速入门教程
    利用JQuery的$.ajax()可以很方便的调用asp.net的后台方法
    函数式编程初探(functional programming)
    用Openfire架设自己的即时聊天服务器
    把DataTable中的身份证号导出excel的解决方案
    jQuery Ajax 全解析
    总结使用Cookies的一些问题
    VS2008 AJAX控件介绍
    支付宝服务接口,实现虚拟交易和实物交易
    Qt QElapsedTimer
  • 原文地址:https://www.cnblogs.com/xiaoheimiaoer/p/3296550.html
Copyright © 2020-2023  润新知