In this example, a
sayHello function) to display an alert dialog box.
BTN1 invokes the
sayHello function using a local variable
localCustName variable contains the text “José”.
BTN1 invokes the same function using an externally defined variable
remoteCustName variable also contains the text “José”.
// this file is encoded as charset = 8859-1 var remoteCustName = "José";
When you load the
Suppose you click BTN 1. You should see this:
In this example, the HTML file is UTF-8. Also, the localCustName variable begins as UTF-8 in the HTML file itself, and the interpreter converts it from UTF-8 into its own charset encoding — which is conveniently also Unicode.
Now let’s imagine you click BTN 2. You should see this:
What’s the fix?
We can fix this by simply telling the interpreter explicitly what the JS file encoding is. The following revised HTML file does this:
Now, when we click on either BTN 1 or BTN 2, we see the same thing:
- Always use the charset attribute in script tags.