html表单的编码及ie下面的hack
来源:互联网 发布:淘宝德国铁元是真的吗 编辑:程序博客网 时间:2024/05/19 01:28
问题是这样的,我的代码是utf8编码的,但是和我的程序接口的程序必须接收gb2312的编码格式,由于我这边不方便使用ajax的方式,因此我在form里加了charset,在FF等浏览器都没问题,但是IE却一直没有效果。
代码如下:
- <form action="#" accept-charset="GB2312" >
- <input name="test" value="测试" readonly>
- <input type=submit>
- </form>
做了下测试,下面的方法应该可以,供参考:
- <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="☠" />
推荐看一下这里,希望对你有帮助。
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.
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>!
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
- html表单的编码及ie下面的hack
- 本地存储在IE下面的编码问题及解决方法
- IE的HTML HACK条件注释
- 在IE下面指定表单编码方式
- 在IE下面指定表单编码方式
- 常用的IE Hack
- CSS IE的HACK
- IE之间的HACK
- IE的hack技术
- IE下的hack
- 【Bootstrap】html属性顺序的编码规范及表单
- 各种IE下的hack
- IE下的CSS hack
- 针对IE的CSS hack
- IE 下面的firebug
- IE下面的Firebug
- 在IE下面指定表单提交编码方式
- HTML语言编码charset不要写在标题<title>的下面
- 游戏服务器技术
- double浮点数转字符串算法
- servfox分析
- a标签中 href onclick
- 韩信点兵(剩余定理)
- html表单的编码及ie下面的hack
- 链表的插入排序
- HDU 1026 Ignatius and the Princess I
- 时间类
- 等一场春,行走在心上
- ubuntu server安装图形界面环境
- 字符串与整数、浮点数、无符号整数之间的转换常用函数
- Windows下JRuby on Rails搭建项目&使用Tomcat部署
- 特征变换(7)总结
在提交页面中字符编码最好与后台处理编码一致。 – tonyzhang 2012-08-23