开发者

Is it possible to set two encodings for one HTML?

Is there a way to specify certain part of a html file as another encoding?

The default encoding for the (generated) html is utf-8. However, some of the included data to be inserted in the html is in another encoding. It's something like:

 <div>
     the normal html in utf-8
 </div>

 <div>
     <%= raw_data_in_another_encoding %>
 </div>

Is there a way to hint a browser to render the 2nd <div>开发者_如何学编程; in another encoding? thanks


No, the entire file must have a single encoding. If you're saving a plain .html file, you'll have to convert the entire file to one encoding.

If you're using a server-side scripting language, however, you can always convert text from one encoding to another. You might designate UTF-8 as the encoding for the page, and then when you encounter bits of content currently encoded in, say, latin1, you can simply convert it to UTF-8 before outputting it.

How you do that, of course, would depend on the particular server-side language you're using.

In PHP, you could do:

echo iconv('ISO-8859-1', 'UTF-8', $someLatin1Text);


You can send any arbitrary encoding at any point in your HTTP response stream, but generally your client won't be able to deal with it. In HTML, multiple encodings in the same document simply aren't permitted. Or even gracefully handled by any modern client except perhaps by accident.

If you are using Ruby (guessing based only on your naming conventions), you can convert a string from one encoding to another using the iconv library. If you're using something else, there's most likely a similar alternative. PHP and Python both offer some encoding translation options based on the iconv library. In the .Net Framework, you can use the Encoding class to grab the suitable source encoding, and call GetBytes with your source byte array as the parameter to get a string suitable for further manipulation.

Numerical character references are another option, if you are primarily using another encoding and only occasionally using characters outside of that encoding's supported range. However, you're generally going to stay saner by converting to and from UTF-8 from legacy encodings.


I think you can't but if you need some text be showed in a different encoding, you can do a "translating function". I had a similar problem with an english page where I had to add some spannish messages, so I do something like this:

function spanishEncoding (string) {
    var res = string;
    res = res.replace( /á/g, "\u00e1" );
    res = res.replace( /Á/g, "\u00c1" );
    res = res.replace( /é/g, "\u00e9" );
    res = res.replace( /É/g, "\u00c9" );
    res = res.replace( /í/g, "\u00ed" );
    res = res.replace( /Í/g, "\u00cd" );
    res = res.replace( /ó/g, "\u00f3" );
    res = res.replace( /Ó/g, "\u00d3" );
    res = res.replace( /ú/g, "\u00fa" );
    res = res.replace( /Ú/g, "\u00da" );
    res = res.replace( /ñ/g, "\u00f1" );
    res = res.replace( /Ñ/g, "\u00d1" );
    return res; };   

var newDiv = window.content.document.createElement("div"); 
newDiv.appendChild(window.content.document.createTextNode("Esta página")); //This shows "Esta p*Â!*gina"   

var anotherDiv = window.content.document.createElement("div"); 
anotherDiv.appendChild(window.content.document.createTextNode(spanishEncoding("Esta página"))); //This shows "Esta página"

Hope it help you!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜