表单数据提交时的字符编码问题


人老了,以前研究过的东西都忘记了。所以还是记录下来比较好。

  废话不说,翠花,上代码:

  Html代码

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>The Character Encoding for Form Submission</title> 
<script> 
function $(id) { 
  return document.getElementById(id) 
} 
 
function createPropertyEditor(name) { 
  var p = document.createElement('p') 
  p.appendChild(document.createTextNode(name + ' = ')) 
  var input = document.createElement('input') 
  input.type = 'text' 
  var get = new Function('this.value = String($name); this.select()'.replace(/$name/g, name)) 
  get.call(input) 
  input.onfocus = get 
  input.onchange = new Function('    
    try {               
      $name = this.value      
    } catch(e) {           
      alert(e.message)       
    } finally {            
      this.value = String($name)  
    }                 
  '.replace(/$name/g, name)) 
  p.appendChild(input) 
  $('properties').appendChild(p) 
} 
 
var form 
 
window.onload = function () { 
  form = document.forms[0] 
  createPropertyEditor('document.charset') 
  createPropertyEditor('document.defaultCharset') 
  createPropertyEditor('document.characterSet') 
  createPropertyEditor('document.inputEncoding') 
  createPropertyEditor('document.xmlEncoding') 
// createPropertyEditor('document.mimeType') 
  createPropertyEditor('form.acceptCharset') 
  createPropertyEditor('form.method') 
  createPropertyEditor('form.enctype') 
// createPropertyEditor('form.encoding') 
} 
</script> 
</head> 
<body> 
<form> 
  <input name="_charset_" type="hidden"> 
  <input name="test" type="text" value="&#31616;&#20307;-&#32321;&#39636;-English">  
  <input type="submit"> 
  <div id="properties"></div> 
</form> 
</body> 
</html> 
如果你总是使用utf-8编码,则基本不用为编码问题伤脑筋。但是总有些时候,事情超出了你的掌控范围。

  通常表单提交时将按照文档本身的编码进行编码。如果是一个gb2312的文档,如何按照utf-8编码提交呢?此外是否可以通知服务器,请求是按照何种方式编码的呢?

  以上代码测试了浏览器为此类问题所提供的各种特性。可将上述代码保存为不同的编码,如UTF-8、GB2312、Big5等(可以加上meta httpequiv=content-type content=text/html;charset=xxx),然后进行测试(即修改document.charset和form.acceptCharset然后提交,观察URL参数的结果)。

  以下是一些浏览器的测试结果(仅测试了GET请求):

  Text代码

  支持document.charset 支持form.acceptCharset 支持_charset_ 

IE6    Y           N*            Y 
FF2    N           Y            Y 
Safari3  Y           Y            N 
Chrome1  Y           Y            N 
Opera9   N*          Y            Y 

  由于手头安装的浏览器有限,有兴趣的同志可帮忙测试一下IE5、IE7、IE8、FF3以及其他浏览器。

  说明:

  1. document.charset

  可获得文档的字符编码。

  也可改写,从而影响到表单提交时所采用的编码。在IE中改变charset还会影响网页的字体。

  IE专有属性,Webkit引擎也实现了该属性。FF2不支持,虽然有一个document.characterSet属性,但是只读。Opera9虽然可通过charset属性读取字符编码,但是改写该属性似无意义。


  2. form.acceptCharset

  按照HTML4规范,FORM上的accept-charset属性(DOM中为acceptCharset)表示服务器可接受的编码列表(以空格或逗号分割多种编码)。浏览器应选取其中一种ASCII兼容的编码(具体选择哪种,各浏览器自行决定——比如chrome虽然也用webkit引擎,但是在选择编码方面似乎与safari有一些不同)作为表单数据的编码。

  IE6虽然有acceptCharset属性,但是无效果。(然而据一封古老的微软工程师在unicode组织邮件列表里的邮件表明,IE5也许支持?有兴趣同志可以测试一下。)

  3. _charset_

  当表单包含一个name为_charset_的hidden类型input,表单提交时,_charset_的value会被设为表单数据的字符编码。

  IE5开始支持此特性。FF团队follow了此特性(https://bugzilla.mozilla.org/show_bug.cgi?id=18643)。

  上述3个特性皆纳入了HTML5规范草案。

人老了,以前研究过的东西都忘记了。所以还是记录下来比较好。

  废话不说,翠花,上代码:

  Html代码

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>The Character Encoding for Form Submission</title> 
<script> 
function $(id) { 
  return document.getElementById(id) 
} 
 
function createPropertyEditor(name) { 
  var p = document.createElement('p') 
  p.appendChild(document.createTextNode(name + ' = ')) 
  var input = document.createElement('input') 
  input.type = 'text' 
  var get = new Function('this.value = String($name); this.select()'.replace(/$name/g, name)) 
  get.call(input) 
  input.onfocus = get 
  input.onchange = new Function('    
    try {               
      $name = this.value      
    } catch(e) {           
      alert(e.message)       
    } finally {            
      this.value = String($name)  
    }                 
  '.replace(/$name/g, name)) 
  p.appendChild(input) 
  $('properties').appendChild(p) 
} 
 
var form 
 
window.onload = function () { 
  form = document.forms[0] 
  createPropertyEditor('document.charset') 
  createPropertyEditor('document.defaultCharset') 
  createPropertyEditor('document.characterSet') 
  createPropertyEditor('document.inputEncoding') 
  createPropertyEditor('document.xmlEncoding') 
// createPropertyEditor('document.mimeType') 
  createPropertyEditor('form.acceptCharset') 
  createPropertyEditor('form.method') 
  createPropertyEditor('form.enctype') 
// createPropertyEditor('form.encoding') 
} 
</script> 
</head> 
<body> 
<form> 
  <input name="_charset_" type="hidden"> 
  <input name="test" type="text" value="&#31616;&#20307;-&#32321;&#39636;-English">  
  <input type="submit"> 
  <div id="properties"></div> 
</form> 
</body> 
</html> 
如果你总是使用utf-8编码,则基本不用为编码问题伤脑筋。但是总有些时候,事情超出了你的掌控范围。

  通常表单提交时将按照文档本身的编码进行编码。如果是一个gb2312的文档,如何按照utf-8编码提交呢?此外是否可以通知服务器,请求是按照何种方式编码的呢?

  以上代码测试了浏览器为此类问题所提供的各种特性。可将上述代码保存为不同的编码,如UTF-8、GB2312、Big5等(可以加上meta httpequiv=content-type content=text/html;charset=xxx),然后进行测试(即修改document.charset和form.acceptCharset然后提交,观察URL参数的结果)。

  以下是一些浏览器的测试结果(仅测试了GET请求):

  Text代码

  支持document.charset 支持form.acceptCharset 支持_charset_ 

IE6    Y           N*            Y 
FF2    N           Y            Y 
Safari3  Y           Y            N 
Chrome1  Y           Y            N 
Opera9   N*          Y            Y 

  由于手头安装的浏览器有限,有兴趣的同志可帮忙测试一下IE5、IE7、IE8、FF3以及其他浏览器。

  说明:

  1. document.charset

  可获得文档的字符编码。

  也可改写,从而影响到表单提交时所采用的编码。在IE中改变charset还会影响网页的字体。

  IE专有属性,Webkit引擎也实现了该属性。FF2不支持,虽然有一个document.characterSet属性,但是只读。Opera9虽然可通过charset属性读取字符编码,但是改写该属性似无意义。


  2. form.acceptCharset

  按照HTML4规范,FORM上的accept-charset属性(DOM中为acceptCharset)表示服务器可接受的编码列表(以空格或逗号分割多种编码)。浏览器应选取其中一种ASCII兼容的编码(具体选择哪种,各浏览器自行决定——比如chrome虽然也用webkit引擎,但是在选择编码方面似乎与safari有一些不同)作为表单数据的编码。

  IE6虽然有acceptCharset属性,但是无效果。(然而据一封古老的微软工程师在unicode组织邮件列表里的邮件表明,IE5也许支持?有兴趣同志可以测试一下。)

  3. _charset_

  当表单包含一个name为_charset_的hidden类型input,表单提交时,_charset_的value会被设为表单数据的字符编码。

  IE5开始支持此特性。FF团队follow了此特性(https://bugzilla.mozilla.org/show_bug.cgi?id=18643)。

  上述3个特性皆纳入了HTML5规范草案。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3