Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save non-carbondated/78a1b39180dd8dfbc0ae754094744c3d to your computer and use it in GitHub Desktop.
Save non-carbondated/78a1b39180dd8dfbc0ae754094744c3d to your computer and use it in GitHub Desktop.
Detecting if a file has a byte order mark (BOM) using JavaScript
var reader = new FileReader();
reader.onerror = function (err) {
console.log('Error:', err);
};
reader.onload = function (e) {
var text = reader.result;
debugger;
var buf = new Uint8Array(text);
// check for byte order mark
// 0xef, 0xbb and 0xbf in hex converts to 239, 187 and 191 in decimal
if (buf[0] === 239 && buf[1] === 187 && buf[2] === 191) { // check for byte order mark
console.log('File has byte order mark (BOM)');
}
};
reader.readAsArrayBuffer(file);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment