如何将JavaScript字符串转码为ISO-8859-1? [英] How do I transcode a Javascript string to ISO-8859-1?

查看:331
本文介绍了如何将JavaScript字符串转码为ISO-8859-1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在撰写一个适用于使用ISO-8859-1的网站的Chrome扩展程序。只是为了提供一些背景知识,我的扩展功能是通过添加更方便的发布表单来更快速地在网站论坛中发布内容。然后通过Ajax调用(使用jQuery)发送编写消息的textarea的值。



如果消息包含之类的字符, 这些字符在发布的消息中显示为á强制浏览器显示UTF-8而非ISO-8859-1,这使得á能正确显示。



它我的理解是Javascript使用UTF-8作为字符串,所以我的理论是,如果在发送字符串之前将字符串转码为ISO-8859-1,它应该可以解决我的问题。然而,似乎没有直接的方法来在Javascript中执行此代码转换,并且我无法触摸服务器端代码。任何建议吗?



我已经尝试将创建的表单设置为使用iso-8859-1,如下所示:

  var form = document.createElement(form); 
form.enctype =application / x-www-form-urlencoded; charset = ISO-8859-1;

还有:

  var form = document.createElement(form); 
form.encoding =ISO-8859-1;

但这似乎不起作用。



编辑:

这个问题实际上在于jQuery如何对邮件进行urlencoding(或者其他方法),我通过告诉jQuery不要处理数据并自己做,如下所示:

();
msg = escape(msg).replace(/ \ + / g,%2B);
.ajax({
type:POST,
url:url,
processData:false,
data:message =+ msg +& amp ; post = Preview Message,
success:function(html){
// ...
},
dataType:html,
contentType: application / x-www-form-urlencoded
});
}


解决方案


这是我的理解,JavaScript使用UTF-8的字符串


否,否。



每个页面都有它在meta标签中定义的charset enconding 元素

 < HEAD> 
< meta http-equiv =content-typecontent =text / html; charset = UTF-8/>

 < HEAD> 
< meta http-equiv =content-typecontent =text / html; charset = ISO-8859-1/>

除此之外,每个页面都应该使用目标字符集编码进行编辑。否则,它将无法按预期工作。



在服务器端定义目标字符集编码是一个好主意。

  Java 
<%@ page pageEncoding =UTF-8contentType =text / html; charset = UTF-8%>

PHP
header(Content-Type:text / html; charset = UTF-8);

C#
我不知道如何...

设置每个脚本文件是否使用敏感字符(á,é,í,ó,ú等等)可能是一个好主意。

 < script type =text / javascriptcharset =UTF-8src =/ PATH / TO / FILE.js>< / script> 

...


因此,我的理论是,如果在发送字符串之前将字符串转码为ISO-8859-1,它应该解决我的问题。

不,不。



目标服务器可以处理除ISO-8859-1之外的字符串。例如,无论您如何设置页面,Tomcat都可以在ISO-8859-1中处理。因此,在服务器端,您可能需要根据设置页面的方式来设置请求。

  Java 
request.setCharacterEncoding(UTF-8)

PHP
//我不知道如何......

如果您确实想翻译目标字符集编码,请按以下步骤操作:

  InternetExplorer 
formElement.encoding =application / x-www-form-urlencoded; charset = ISO-8859-1;
ELSE
formElement.enctype =application / x-www-form-urlencoded; charset = ISO-8859-1;

或者,您应该提供一个函数,以Unicode字符集,由每个字符使用。无论目标字符集编码如何,它都可以工作。例如,Unicode字符集是\00E1;

  alert(没有Unicode字符集数字表示法) ; 
函数convertToUnicodeCharacterSet(value){
if(value ==á)
return\\\á;
}
alert(在Unicode字符集中的数字表示是:+ convertToUnicodeCharacterSet(á));

这里你可以看到在行动:



你可以使用这个链接作为指南(请参阅JavaScript转义)



添加到原始答案我如何实现jQuery funcionality

  var dataArray = $(formElement).serializeArray(); 
var queryString =;
for(var i = 0; i< dataArray.length; i ++){
queryString + =& + dataArray [i] [name] +++ encodeURIComponent(dataArray [i] [value]);
$ b $ .ajax({
url:url.htm,
data:dataString,
contentType:application / x-www-form-urlencoded ; charset = UTF-8,
成功:函数(响应){
//进程响应
});
});

工作正常,没有任何头痛。

关心,


I'm writing a Chrome extension that works with a website that uses ISO-8859-1. Just to give some context, what my extension does is making posting in the site's forums quicker by adding a more convenient post form. The value of the textarea where the message is written is then sent through an Ajax call (using jQuery).

If the message contains characters like á these characters appear as á in the posted message. Forcing the browser to display UTF-8 instead of ISO-8859-1 makes the á appear correctly.

It is my understanding that Javascript uses UTF-8 for its strings, so it is my theory that if I transcode the string to ISO-8859-1 before sending it, it should solve my problem. However there seems to be no direct way to do this transcoding in Javascript, and I can't touch the server side code. Any advice?

I've tried setting the created form to use iso-8859-1 like this:

var form = document.createElement("form");
form.enctype = "application/x-www-form-urlencoded; charset=ISO-8859-1";

And also:

var form = document.createElement("form");
form.encoding = "ISO-8859-1";

But that doesn't seem to work.

EDIT:

The problem actually lied in how jQuery was urlencoding the message (or something along the way), I fixed this by telling jQuery not to process the data and doing it myself as is shown in the following snippet:

function cfaqs_post_message(msg) {
  var url = cfaqs_build_post_url();
  msg = escape(msg).replace(/\+/g, "%2B");
  $.ajax({
    type: "POST",
    url: url,
    processData: false,
    data: "message=" + msg + "&post=Preview Message",
    success: function(html) {
      // ...
    },
    dataType: "html",
    contentType: "application/x-www-form-urlencoded"
  });
}

解决方案

It is my understanding that Javascript uses UTF-8 for its strings

No, no.

Each page has its charset enconding defined in meta tag, just below head element

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

or

<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"/>

Besides that, each page should be edited with the target charset encoding. Otherwise, it will not work as expected.

And it is a good idea to define its target charset encoding on server side.

Java
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>

PHP
header("Content-Type: text/html; charset=UTF-8");

C#
I do not know how to...

And it could be a good idea to set up each script file whether it uses sensitive characters (á, é, í, ó, ú and so on...).

<script type="text/javascript" charset="UTF-8" src="/PATH/TO/FILE.js"></script>

...

So it is my theory that if I transcode the string to ISO-8859-1 before sending it, it should solve my problem

No, no.

The target server could handle strings in other than ISO-8859-1. For instance, Tomcat handles in ISO-8859-1, no matter how you set up your page. So, on server side, you could have to set up your request according how your set up your page.

Java
request.setCharacterEncoding("UTF-8")

PHP
// I do not know how to...

If you really want to translate the target charset encoding, TRY as follows

InternetExplorer
    formElement.encoding = "application/x-www-form-urlencoded; charset=ISO-8859-1";
ELSE
    formElement.enctype  = "application/x-www-form-urlencoded; charset=ISO-8859-1";

Or you should provide a function that gets the numeric representation, in Unicode Character Set, used by each character. It will work regardless of the target charset encoding. For instance, á as Unicode Character Set is \u00E1;

alert("á without its Unicode Character Set numerical representation");
function convertToUnicodeCharacterSet(value) {
    if(value == "á")
        return "\u00E1";
}
alert("á Numerical representation in Unicode Character Set is: " + convertToUnicodeCharacterSet("á"));

Here you can see in action:

You can use this link as guideline (See JavaScript escapes)

Added to original answer how I implement jQuery funcionality

var dataArray = $(formElement).serializeArray();
var queryString = "";
for(var i = 0; i < dataArray.length; i++) {
    queryString += "&" + dataArray[i]["name"] + "+" + encodeURIComponent(dataArray[i]["value"]);
}
$.ajax({
    url:"url.htm",
    data:dataString,
    contentType:"application/x-www-form-urlencoded; charset=UTF-8",
    success:function(response) {
        // proccess response
    });
});

It works fine without any headache.

Regards,

这篇关于如何将JavaScript字符串转码为ISO-8859-1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆