Reputation: 433
I am trying to understand how the charset attribute works but my charset attribute is ignored. I will add my examples.
1) I have got an HTML file with meta tag with char attribute.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<meta charset="windows-1254">
</head>
<body>
<p>Αποτελεί την πλέον επιτυχημένη ομάδα στην ιστορία του</p>
</body>
</html>
The file is saved with the option "UTF-8". However I ask the browser to show it in Turkish encoding (Windows-1254). What I show is the correct version, Greek letters are shown properly. charset="windows-1254" seems ignored
2) I have got another example with script tag. I use the JavaScript file saved with the option "UTF-8"
document.write("Αποτελεί την πλέον επιτυχημένη ομάδα στην ιστορία του");
The HTML file calling it uses tag with charset value windows-1254. The HTML file was saved with the option ANSI, (it seems it saves it as windows-1254)
<!DOCTYPE html>
<html>
<head>
<title>JS Dosyası Örneği</title>
</head>
<body>
<script src="javascript.js" charset="windows-1254"></script>
</body>
</html>
JS file is shown correctly, charset="windows-1254" seems ignored again.
3) I have got another example with script tag. I use the JavaScript file saved with the option "UTF-8"
document.write("Örnek yazı");
The HTML file calling it uses tag with charset value windows-1254. The HTML file was saved with the option ANSI, (it seems it saves it as windows-1254)
<!DOCTYPE html>
<html>
<head>
<title>JS Dosyası Örneği</title>
</head>
<body>
<script src="javascript.js" charset="windows-1254"></script>
</body>
</html>
JS file is shown correctly, charset="windows-1254" seems not ignored, if I delete charset="windows-1254" it works properly. I don't see an effect of charset attribute. (It doesn't sound weird, it seems IE detects the page is encoded in windows-1254 and JS file is not different)
4) The last example is from W3Schools:
http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_script_charset
If I change the charset value as advised the Greek letters aren't shown correctly in IE. It seems the charset attribute is not ignored.
The question is that "why is my attribute is ignored while the example of W3Schools not?".
Upvotes: 1
Views: 2075
Reputation: 201558
1) It seems that you saved the file using Notepad. When saving a file as UTF-8, Notepad inserts a BOM (Byte Order Mark) at the start. The BOM acts as a “signature” that makes browsers treat the file as UTF-8 encoded, no matter what. This actual behavior is being documented in HTML5 (8.2.2.2 Determining the character encoding; technicality alert!). So the meta
tag is ignored.
2) As in 1. The BOM at the start of the JS file makes browsers treat it as UTF-8 encoded. The encoding of the HTML document is not relevant here.
3) Since the JS file was saved as UTF-8 with BOM, it gets correctly processed independently of the charset
attribute. In this simple example, the encoding of the HTML file and the information about its encoding affects only its title
element (which may be shown outside the document proper).
4) W3schools is unreliable and confusing and should be avoided. It is not clear what they are trying to say and what they are doing, but they seem to render the test document in an iframe
element some way. And normally the charset
attribute matters (and does for me on that page in Firefox), when not overridden by a BOM or by HTTP headers.
Upvotes: 3
Reputation: 1050
I think it is browser dependent. Using:
windows-1252
in Chrome caused no change to the Greek letters. However if you try it in IE you get:
αβγδεζηθ
Upvotes: 0