您的位置:首页 > Web前端 > HTML

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

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

代码如下:

<form action="#" accept-charset="GB2312" >

<input name="test" value="测试" readonly>

<input type=submit>

</form>


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

0

<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>

在提交页面中字符编码最好与后台处理编码一致。 – tonyzhang 2012-08-23

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

<HTML>

<HEAD>

<meta http-equiv=content-type content="text/html; charset=UTF-8">

<SCRIPT LANGUAGE="JavaScript">

var isIE=!!window.ActiveXObject;

if(isIE && document.charset!="utf-8")location.reload(false);

if(location.search) alert("编码为:"+location.search.substr(6))

</SCRIPT>

<TITLE>encode before form post</TITLE>

<META NAME="Author" CONTENT="emu">

</HEAD>

<BODY>

<form action="#" accept-charset="GB2312" onsubmit="if(isIE)document.charset='GB2312'">

<input name="test" value="测试" readonly>

<input type=submit>

</form>

</BODY>

</HTML>


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

<input name="iehack" type="hidden" value="☠" />


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

评论 (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="☠" />


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
edited Jul
13 '10 at 15:45




Artefacto

48.1k378123

answered Sep 2 '09 at 12:42

Trygve Lie

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 "ÿ" 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
answered Aug 8 '12 at 11:24





dgaspar

32227

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: