html表单的编码及ie下面的hack

来源:互联网 发布:淘宝德国铁元是真的吗 编辑:程序博客网 时间:2024/05/19 01:28

问题是这样的,我的代码是utf8编码的,但是和我的程序接口的程序必须接收gb2312的编码格式,由于我这边不方便使用ajax的方式,因此我在form里加了charset,在FF等浏览器都没问题,但是IE却一直没有效果。

代码如下:

  1. <form action="#" accept-charset="GB2312" >
  2.         <input name="test" value="测试" readonly>
  3.         <input type=submit>
  4.     </form>
评论 (1) • 分享 • 链接 • 2012-08-20 
  • 0
    <%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
    在提交页面中字符编码最好与后台处理编码一致。
     – tonyzhang 2012-08-23

做了下测试,下面的方法应该可以,供参考:

  1. <HTML>
  2. <HEAD>
  3.     <meta http-equiv=content-type content="text/html; charset=UTF-8">
  4.     <SCRIPT LANGUAGE="JavaScript">
  5.         var isIE=!!window.ActiveXObject;
  6.         if(isIE && document.charset!="utf-8")location.reload(false);
  7.         if(location.search) alert("编码为:"+location.search.substr(6))
  8.     </SCRIPT>
  9.     <TITLE>encode before form post</TITLE>
  10.     <META NAME="Author" CONTENT="emu">
  11. </HEAD>
  12. <BODY>
  13.     <form action="#" accept-charset="GB2312" onsubmit="if(isIE)document.charset='GB2312'">
  14.         <input name="test" value="测试" readonly>
  15.     <input type=submit>
  16.     </form>
  17. </BODY>
  18. </HTML>

可以加入一个隐藏的input,例如:

  1. <input name="iehack" type="hidden" value="&#9760;" />

推荐看一下这里,希望对你有帮助。

评论 (1) • 链接 • 2012-08-20

There is a simple hack to this:

Insert a hidden input field in the form with an entity which only occur in the character set the server your posting (or doing a GET) to accepts.

Example: If the form are located on a server serving ISO-8859-1 and the form will post to a server expecting UTF-8 insert something like this in the form:

<input name="iehack" type="hidden" value="&#9760;" />

IE will then "detect" that the form contains a UTF-8 character and use UFT-8 when you POST or GET. Strange, but it does work.

share|improve this answer
 
2 
This does not seem to work for the inverse situation. Posting a form which stays on a utf-8 page to an iso-8859-1 page on an other server. –  MaxiWheat Jan 11 '10 at 16:59
1 
Right, it won't work the other way. Wenn you specify something like "&#255;" which is the "highest" Char in the ISO-8859-1 charset, this obviously is an UTF-8 code, too. So, IE therefore thinks it's UTF-8. So it only works when the target charset has a greater amount of characters. –  acme Oct 19 '10 at 8:41
 
this solved my problem when IE8 and IE9 weren't respecting accept-charset='utf-8', thanks! –  andy magoonDec 7 '11 at 16:32
 
Doesn't work for me in IE8, please see the testcase –  Tomas Oct 31 '13 at 8:41 



ith decent browsers:

<form accept-charset="ISO-8859-1" .... >

With IE (any):

document.charset = 'ISO-8859-1'; // do this before submitting your non-utf8 <form>!
share|improve this answer
 
3 
This seems to solve the IE problem (when JavaScript is enabled in the browser), and you can implement it by using the attribute onsubmit="document.charset = 'ISO-8859-1'" in the form tag. – Jukka K. Korpela Aug 30 '12 at 12:15















0 0
原创粉丝点击