Michael Harvey Michael Harvey - 5 months ago 13
HTML Question

Chrome thinks my JavaScript is Mandarin

When I compile and run my typescript and load the page in google chrome, google chrome thinks my javascript is mandarin.

Here's my .ts file:

class Student {
fullName: string;
constructor(public firstName, public middleInitial, public lastName) {
this.fullName = firstName + " " + middleInitial + " " + lastName;
}
}


interface Person {
firstName: string;
lastName: string;
}


function greeter(person: Person) {
return "Hello, " + person.firstName + person.lastName;
}

var user = new Student("Mike", "A", "Harv");

document.body.innerHTML = greeter(user);


Here's my .js file:

var Student = (function () {
function Student(firstName, middleInitial, lastName) {
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
this.fullName = firstName + " " + middleInitial + " " + lastName;
}
return Student;
}());
function greeter(person) {
return "Hello, " + person.firstName + person.lastName;
}
var user = new Student("Mike", "A", "Harv");
document.body.innerHTML = greeter(user);


Here's my .html file:

<!DOCTYPE html>
<html>
<head><title>TypeScript Greeter</title></head>
<body>
<script src="greeter.js"></script>
</body>
</html>


enter image description here

enter image description here

Edit: The encoding on my .js file is UTF-8

enter image description here

Hex-dump of my js file.

enter image description here

Answer

The Mandarin letters you see will appear if you decode the utf-8 (without bom) encoded greeter.js file as UCS2-LE. I tried to convince Chrome to decode the utf8 encoded file as ucs2le, but I didnt managed to. It looks every program, including chrome will only use UCS2-LE if the right BOM is present at the beginning of the file. The following bytes FF FE 76 61 72 20 53 74 ... (These bytes are the same as in your hexdump, with FF FE prepended) will be decoded to:

enter image description here

These are the same characters as on your screenshot.

Given all this, I am pretty sure that your greeter.js files encoding is broken, it has the UCS2 bom FF FE, and the utf8 encoded bytes of the javascript are appended.

Comments