I want to read a file from my server with javascript and display it's content in a html page.
The file is in ANSI charset, and it has romanian characters.. I want to display those characters in the way they are :D not in different black symbols..
So I think my problem is the charset.. I have a get request that takes the content of the file, like this:
function IO(U, V) {//LA MOD String Version. A tiny ajax library. by, DanDavis
var X = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
X.open(V ? 'PUT' : 'GET', U, false );
X.setRequestHeader('Content-Type', 'Charset=UTF-8');
X.send(V ? V : '');return X.responseText;}
As far as I know the romanian characters are included in UTF-8 charset so I set the charset of the request header to utf-8.. the file is in utf-8 format and I have the meta tag that tells the browser that the page has utf-8 content..
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
So if I query the server the direct file, the browser shows me the romanian characters but if I display the content of the page through this script, I see only symbols instead of characters..
So what I am doing wrong?
Thank you!
PS: I want this to work on Firefox at least not necessarily in all browsers..
While my initial assumption was the same as T.J. Crowder's, a quick chat established that the OP uses some hosting service and cannot easily change the Content-Type headers.
The files were sent as text/plain or text/html without any Charset paramter, hence the browser interprets them as UTF-8 (which is the default).
So saving the files in UTF-8 (instead of ANSI/Windows-1252) did the trick.
You need to ensure that the HTTP response returning the file data has the correct charset identified on it. You have to do that server-side, I don't think you can force it from the client. (When you set the content type in the request header, you're setting the content type of the request, not the response.) So for instance, the response header from the server would be along the lines of:
Content-Type: text/plain; charset=windows-1252
...if by "ANSI" you mean the Windows-1252 charset. That should tell the browser what it needs to do to decode the response text correctly before handing it to the JavaScript layer.
One problem, though: As far as I can tell, Windows-1252 doesn't have the full Romanian alphabet. So if you're seeing characters like Ș, ș, Ţ, ţ, etc., that suggests the source text is not in Windows-1252. Now, perhaps it's okay to drop the diacriticals on those in Romanian (I wouldn't know) and so if your source text just uses S and T instead of Ș and Ţ, etc., it could still be in Windows-1252. Or it may be ISO-8859 or ISO-8859-2 (both of which drop some diacriticals) or possibly ISO-8859-16 (which has full Romanian support). Details here.
So the first thing to do is determine what character set the source text is actually in.
Related
I tried charset UTF-8 to display the ä, it displayed some square box.
Also i tried with charset ISO-8859-1 to display the ä, it diplayed as ä. (which is correct)
But When combine the above both charset within javascript condition, its not working properly. Refer below code,
<html>
<head>
<script type="text/javascript">
var lang = 'German';
function f(){
if(lang != 'SomeOtherLanguage'){
//here code will execute. And page should display square box. Instead of square box, ä is displayed. Which is wrong. I cant able to find reason.
metaTag = '<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>';
}
else
metaTag = '<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>';
document.getElementsByTagName('head')[0].innerHTML += metaTag;
}
</script>
</head>
<body onload="f()">
<h1>Latin letter : ä </h1> <br />
</body>
</html>
You can't, the character set is established by the parser, which needs to parse Javascript in order to generate that meta DOM.
You still can use only one character set and convert the data.
What you are attempting to do will never work.
If the raw bytes of your HTML are not encoded as UTF-8 to begin with, you can't claim UTF-8, in a <meta> tag, or an HTTP Content-Type header. You would be lying to the browser/client, and is why you get bad results.
Your code will "work" only when your <meta> tag claims ISO-8859-1 (and there is no Content-Type header to override that) if your HTML is actually encoded in ISO-8859-1. In several (but not all) of the ISO-8859-X charsets, including ISO-8859-1, ä is encoded as byte 0xE4, so your code "works" when claiming ISO-8859-1 if byte 0x34 is present in the HTML's raw data.
In UTF-8, ä is encoded as bytes 0xC3 0xA4 instead. If your HTML contains byte 0xE4, but you claim UTF-8, you get bad results (0xE4 is not a valid byte in UTF-8).
So, your <meta> tag (and HTTP Content-Type header) needs to claim a charset that actually matches the real encoding of the HTML's raw bytes.
If your HTTP server is serving a static HTML file, the file is encoded in a specific charset when the HTML is saved to file. That same charset needs to be specified statically in the <meta> tag (and preferably also in the HTTP Content-Type header). If your HTTP server is generating the HTML dynamically, it needs to encode the HTML in a specific charset for transmission, so it needs to specify that same charset in the generated <meta> tag (and Content-Type header).
In other words, stop trying to lie to the browser/client. Tell the truth, then you won't run into this problem anymore.
So I'm trying to show utf-8 characters coming from JavaScript.
I should have it all:
header
<meta charset="utf-8">
include js
<script type="text/javascript" src="x.js" charset="utf-8"></script>
File x.js is saved as UTF-8 (and also the other files)
It works with all my PHP files, just not when it comes from a simple alert in JavaScript.
alert("Prénom doit être rempli");
Instead the famous '?' characters are showed in the alert box.
Anything I've forgotten?
Here what you need to do: open your file in notepad and save it again (save as) and this time select UTF-8 from save-file-dialog-box. Your issue will be solved
From the spec:
The charset attribute gives the character encoding of the external script resource. The attribute must not be specified if the src attribute is not present. If the attribute is set, its value must be an ASCII case-insensitive match for one of the labels of an encoding, and must specify the same encoding as the charset parameter of the Content-Type metadata of the external file, if any.
(My emphasis.)
So you need to ensure that your server is sending the correct Content-Type header — either with no charset, or with charset=utf-8.
If your server is already sending the charset as part of the Content-Type, that's a good thing: Just remove the charset attribute from the script tag.
I am integrating yahoo open ID for my site. My site is running for different languages( en, jp, cn etc.). When I am trying to logged in from english site with yahoo opend ID then its fine but when the same this I tried from japanese or chinese site then it's not redirecting me to yahoo open id login.
Each time I am getting the below error from javascript
Error: The character encoding of the plain text document was not
declared. The document will render with garbled text in some browser
configurations if the document contains characters from outside the
US-ASCII range. The character encoding of the file needs to be
declared in the transfer protocol or file needs to use a byte order
mark as an encoding signature. Source File:
http://uatstorefrontjpcr.mobi-book.com/ReturnFromSocial/LogOnYahoo
Line: 0
Can anyone suggest what to do.
I have used SocialAuth-net.dll for this purpose. I have set all required wrapper in web.config. Same coding is okay with Google and facebook open ID.
Your web server is probably sending back a response with a Content-Type of text/plain. When a web browser receives a response with that content type, it doesn't know what encoding should be used to decode it; since you haven't told it how to decode it, different browsers might choose different ways.
The solution is to provide an explicit encoding. For example, if you know that the text is UTF-8 encoded, then you could provide it in a header like so:
Content-Type: text/plain; charset=UTF-8
According to an informational page from the W3C, you can get ASP.Net to include that bit in the header using Response.ContentEncoding. Again using UTF-8 as an example, you can set it like so:
Response.ContentEncoding = Encoding.UTF8;
In HTML, script element has optional charset attribute.
What is the purpose of it?
When is it useful?
If your javascript files are encoded with a different encoding than the page is using, you can use the charset attribute to tell the browser how to interpret it.
For example, if the page is using Latin1, but the JS file was created with UTF-8.
The purpose of the charset parameter is to specify the encoding of the external script in cases where the encoding is not specified at the HTTP protocol level. It is not meant to override encoding information in HTTP headers, and it does not do that.
This is useful when the author cannot control HTTP headers and the headers do not specify character encoding. It is also useful for offline files, such as in a local copy of a web page accessed directly, not via an HTTP server, so that no HTTP headers exist.
In practice, it is not very useful. If you need to use non-Ascii characters in a JavaScript file, you can use UTF-8 encoding. If you use UTF-8 with a leading BOM, the BOM acts as a useful indicator that lets browsers infer the encoding. But it does not hurt to additionally use charset=utf-8.
Each JavaScript file is a separate element from page, after all you can even load JS from some remote author's server that otherwise have no relations to your page at all. Just as with any other external element, you can manually specify "charset" if remote server returns wrong charset for some reason or just to be sure.
Also, if you have write access to this JS file yourself, you may want to replace all non-ASCII with Unicode position escapes - this will guarantee that symbols will always be interpreted correctly, no matter what encoding is specified in headers. Some JS minifiers, like Google Closure Compiler, can do it for you automatically.
i have this example (works in chrome with correct codification, but not in the others browsers)
http://emprego.xtreemhost.com/slide/
how i can solve this? i put the `charset=ISO-8859-1" for the index and the externals html. The problem is the load() method i think
any help?
ps: already tried with utf8
thanks!!
If I access abc.htm as:
http://emprego.xtreemhost.com/slide/abc.htm
then my browser correctly guesses that it is encoded as Latin-1 (AKA ISO-8859-1) but it looks like it is interpreted as UTF-8 when you .load it. If you load abc.htm and play around with the "text encoding" menu (probably under "view" somewhere) in your browser you should see how it appears when interpreted as different encodings.
Have a look at the headers that come back when you load abc.htm:
Content-Type: text/html
And abc.htm does not specify any particular encoding so it is up to the browser to make a guess. You want your Content-Type to look like this:
Content-Type: text/html; charset=iso-8859-1
so that the browser knows what encoding it is dealing with.
I'd convert everything to UTF-8 and arrange to have the character specified in the HTML files and in the Content-Type header.
Put this code on top of the script
$(function() {$.ajaxSetup({beforeSend : function(xhr){xhr.overrideMimeType('text/html; charset=Windows-1250');}});});