charset (HTML attribute)

Spec
Depr. Version
No HTML 4
Browser support (more…)
IE5.5+ FF1+ SA1.3+ OP10 CH2+
Full Full Full None Full

Syntax

charset="character encoding scheme"

Description

The charset attribute defines the character encoding used in a linked script file (which is referred to by the src attribute). You’d use this attribute when the character encoding employed by the external JavaScript file differs from that of the main document. For example, the external script file has the following script:

If the document that links to this eternal file is declared as using "ISO 8859-1" encoding, the alert text would be unreadable nonsense. Adding charset="utf-8" to the script start tag makes it display correctly.

Example

This example shows a script file that’s using a slightly different character encoding from the default "ISO-8859-1":

<script type="text/javascript" src="/scripts/common.js"
  charset="ISO-8859-15"></script>

Value

The charset attribute takes as its value a space and/or a comma-delimited list of character sets, as set out in RFC 2045 (a fun bit of bedtime reading).

Compatibility

Internet Explorer Firefox Safari Opera Chrome
5.5 6.0 7.0 8.0 1.0 1.5 2.0 3.0 3.5 1.3 2.0 3.1 4.0 9.2 9.5 10.0 2.0
Full Full Full Full Full Full Full Full Full Full Full Full Full Full Full None Full

This attribute is generally well supported, but strangely appears to have fallen off the supported attribute list with Opera version 10. Using the simple alert test shown above, earlier versions of Opera displayed the characters correctly, but Opera 10 behaved as if no charset was specified at all, making the alert unreadable.

User-contributed notes

There are no comments yet.

Related Products