0

我尝试使用encodeURI()encodeURIComponent()函数在javascript中对特殊字符进行编码,并使用该java.net.URLDecoder.decode()方法对其进行解码,这就像Firefox中的魅力一样。但它似乎在 Internet Explorer 中不起作用。是否有任何替代代码可以在两个浏览器上使用相同的代码?

例子:

当我作为值传递时$%^&,编码后,它变成%24%25%5E%26. 使用 java.net.URLDecoder.decode() 方法解码后,变为$%%5E&

这是实际值-

var str = "$%^&";
var valueJS = encodeURI(str);
var valueJS = encodeURIComponent(valueJS); // to encode even those chars in valueJS that were not encoded by encodeURI()

这是编码值-

String value = "%2524%2525%255E%2526";
while(value.matches(".*%25[A-Za-z0-9]*")) {
     value = value.replace("%25", "%"); // manually trying to achieve %24%25%5E%26
}
value = java.net.URLDecoder(value, "UTF-8");

// I was expecting the decoded value to be $%^&, but it turns out to be $%%5E&
4

1 回答 1

1

修复。添加了 2 个<meta>标签

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

并使用 javascriptescape()而不是encodeURI()andencodeURIComponent()

于 2013-03-12T12:16:52.603 回答