Created
December 4, 2017 17:27
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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