I’m trying to decode a base64 string for an image back into binary so it can be downloaded and displayed locally by an OS.
The string I have successfully renders when put as the src of an HTML IMG element with the data URI preface (data: img/png;base64, ) but when using the atob function or a goog closure function it fails.
However decoding succeeds when put in here: http://www.base64decode.org/
Any ideas?
EDIT:
I successfully got it to decode with another library other than the built-in JS function. But, it still won’t open locally — on a Mac says it’s damaged or in an unknown format and can’t get opened.
The code is just something like:
imgEl.src = 'data:img/png;base64,' + contentStr; //this displays successfully
decodedStr = window.atob(contentStr); //this throws the invalid char exception but i just
//used a different script to get it decode successfully but still won't display locally
the base64 string itself is too long to display here (limit is 30,000 characters)
When I was developing on http://web.twtxt.org I ran into this mysterious javascript error:
InvalidCharacterError: DOM Exception 5: An invalid or illegal character was specified, such as in an XML name.
atobbundle.js:245
(anonyme Funktion)bundle.js:245
emitbundle.js:1119
handlebundle.js:1673
onreadystatechangebundle.js:1465
The source code in question took a base64 encoded content of the github api and converted it with window.atob
into an utf8 string.
This worked well except for safari on iOS!
Thanks to Ross117 at SO I looked further at the base64 encoded string:
atob("MjAxNi0wMy0xOFQxOToxNzozOC4xMTNaCS9uaWNrIGRyYWNvYmx1ZQoyMDE2nLTAzLTE4VDE5OjE3OjM4LjExM1oJL3R3dHVybCBodHRwczovL2RyYWNvYmx1");
// error: DOM Exception 5: An invalid or illegal character was specified, such as in an XML name.
As you can see there is a small n
within the string. Chrome manages to ignore that, but Safari fails with this DOM Exception 5
error.
So this is my fix:
var input = "MjAxNi0wMy0xOFQxOToxNzozOC4xMTNaCS9uaWNrIGRyYWNvYmx1ZQoyMDE2nLTAzLTE4VDE5OjE3OjM4LjExM1oJL3R3dHVybCBodHRwczovL2RyYWNvYmx1";
var output = atob(input.replace(/s/g, ""));
// works!
to get rid of the whitespaces. I filled a support request at github, to ask if they can remove the additional n
in their
api response, since it seems like it is not necessary at all.
org.w3c.dom
Class DOMException
java.lang.Object | +--java.lang.Throwable | +--java.lang.Exception | +--java.lang.RuntimeException | +--org.w3c.dom.DOMException
- public class DOMException
- extends java.lang.RuntimeException
DOM operations only raise exceptions in «exceptional» circumstances, i.e.,
when an operation is impossible to perform (either for logical reasons, because
data is lost, or because the implementation has become unstable). In general,
DOM methods return specific error values in ordinary processing situations,
such as out-of-bound errors when using NodeList
.
Implementations should raise other exceptions under other circumstances. For
example, implementations should raise an implementation-dependent exception if
a null
argument is passed.
Some languages and object systems do not support the concept of exceptions.
For such systems, error conditions may be indicated using native error
reporting mechanisms. For some bindings, for example, methods may return error
codes similar to those listed in the corresponding method descriptions.
See also the Document Object
Model (DOM) Level 2 Core Specification.
- See Also:
- Serialized
Form
Field Summary | |
short |
code |
static short |
DOMSTRING_SIZE_ERR If the specified range of text does not fit into a DOMString |
static short |
HIERARCHY_REQUEST_ERR If any node is inserted somewhere it doesn’t belong |
static short |
INDEX_SIZE_ERR If index or size is negative, or greater than the allowed value |
static short |
INUSE_ATTRIBUTE_ERR If an attempt is made to add an attribute that is already in use elsewhere |
static short |
INVALID_ACCESS_ERR If a parameter or an operation is not supported by the underlying object. |
static short |
INVALID_CHARACTER_ERR If an invalid or illegal character is specified, such as in a name. |
static short |
INVALID_MODIFICATION_ERR If an attempt is made to modify the type of the underlying object. |
static short |
INVALID_STATE_ERR If an attempt is made to use an object that is not, or is no longer, usable. |
static short |
NAMESPACE_ERR If an attempt is made to create or change an object in a way which is incorrect with regard to namespaces. |
static short |
NO_DATA_ALLOWED_ERR If data is specified for a node which does not support data |
static short |
NO_MODIFICATION_ALLOWED_ERR If an attempt is made to modify an object where modifications are not allowed |
static short |
NOT_FOUND_ERR If an attempt is made to reference a node in a context where it does not exist |
static short |
NOT_SUPPORTED_ERR If the implementation does not support the requested type of object or operation. |
static short |
SYNTAX_ERR If an invalid or illegal string is specified. |
static short |
WRONG_DOCUMENT_ERR If a node is used in a different document than the one that created it (that doesn’t support it) |
Constructor Summary |
|
Methods inherited from class java.lang.Throwable |
fillInStackTrace, getLocalizedMessage, getMessage, printStackTrace, |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, |
code
public short code
INDEX_SIZE_ERR
public static final short INDEX_SIZE_ERR
- If index or size is negative, or greater than the allowed value
DOMSTRING_SIZE_ERR
public static final short DOMSTRING_SIZE_ERR
- If the specified range of text does not fit into a DOMString
HIERARCHY_REQUEST_ERR
public static final short HIERARCHY_REQUEST_ERR
- If any node is inserted somewhere it doesn’t belong
WRONG_DOCUMENT_ERR
public static final short WRONG_DOCUMENT_ERR
- If a node is used in a different document than the one that created it
(that doesn’t support it)
INVALID_CHARACTER_ERR
public static final short INVALID_CHARACTER_ERR
- If an invalid or illegal character is specified, such as in a name. See
production 2 in the XML specification for the definition of a legal character,
and production 5 for the definition of a legal name character.
NO_DATA_ALLOWED_ERR
public static final short NO_DATA_ALLOWED_ERR
- If data is specified for a node which does not support data
NO_MODIFICATION_ALLOWED_ERR
public static final short NO_MODIFICATION_ALLOWED_ERR
- If an attempt is made to modify an object where modifications are not
allowed
NOT_FOUND_ERR
public static final short NOT_FOUND_ERR
- If an attempt is made to reference a node in a context where it does not
exist
NOT_SUPPORTED_ERR
public static final short NOT_SUPPORTED_ERR
- If the implementation does not support the requested type of object or
operation.
INUSE_ATTRIBUTE_ERR
public static final short INUSE_ATTRIBUTE_ERR
- If an attempt is made to add an attribute that is already in use
elsewhere
INVALID_STATE_ERR
public static final short INVALID_STATE_ERR
- If an attempt is made to use an object that is not, or is no longer,
usable. -
- Since:
- DOM Level 2
SYNTAX_ERR
public static final short SYNTAX_ERR
- If an invalid or illegal string is specified.
-
- Since:
- DOM Level 2
INVALID_MODIFICATION_ERR
public static final short INVALID_MODIFICATION_ERR
- If an attempt is made to modify the type of the underlying object.
-
- Since:
- DOM Level 2
NAMESPACE_ERR
public static final short NAMESPACE_ERR
- If an attempt is made to create or change an object in a way which is
incorrect with regard to namespaces. -
- Since:
- DOM Level 2
INVALID_ACCESS_ERR
public static final short INVALID_ACCESS_ERR
- If a parameter or an operation is not supported by the underlying
object. -
- Since:
- DOM Level 2
DOMException
public DOMException(short code, java.lang.String message)
Copyright
© 2000 W3C® (MIT, INRIA,
Keio), All Rights Reserved. W3C liability,
trademark,
document
useand software
licensing rules apply. Your interactions with this site are in accordance
with our public
and Member
privacy statements.
-
public interface DOMConstants
String constants used by the DOM classes.
-
-
Field Summary
Fields
Modifier and Type Field and Description static java.lang.String
DOM_CANONICAL_FORM_PARAM
static java.lang.String
DOM_CDATA_SECTIONS_PARAM
static java.lang.String
DOM_CDATA_SECTIONS_SPLITTED_ERROR
static java.lang.String
DOM_CHECK_CHARACTER_NORMALIZATION_PARAM
static java.lang.String
DOM_COMMENTS_PARAM
static java.lang.String
DOM_DATATYPE_NORMALIZATION_PARAM
static java.lang.String
DOM_ELEMENT_CONTENT_WHITESPACE_PARAM
static java.lang.String
DOM_ENTITIES_PARAM
static java.lang.String
DOM_ERROR_HANDLER_PARAM
static java.lang.String
DOM_INFOSET_PARAM
static java.lang.String
DOM_INVALID_CHARACTER_ERROR
static java.lang.String
DOM_INVALID_CHARACTER_IN_NODE_NAME_ERROR
static java.lang.String
DOM_NAMESPACE_DECLARATIONS_PARAM
static java.lang.String
DOM_NAMESPACES_PARAM
static java.lang.String
DOM_NORMALIZE_CHARACTERS_PARAM
static java.lang.String
DOM_SPLIT_CDATA_SECTIONS_PARAM
static java.lang.String
DOM_VALIDATE_IF_SCHEMA_PARAM
static java.lang.String
DOM_VALIDATE_PARAM
static java.lang.String
DOM_WELL_FORMED_PARAM
-
-
-
Field Detail
-
DOM_CANONICAL_FORM_PARAM
static final java.lang.String DOM_CANONICAL_FORM_PARAM
- See Also:
- Constant Field Values
-
DOM_CDATA_SECTIONS_PARAM
static final java.lang.String DOM_CDATA_SECTIONS_PARAM
- See Also:
- Constant Field Values
-
DOM_CHECK_CHARACTER_NORMALIZATION_PARAM
static final java.lang.String DOM_CHECK_CHARACTER_NORMALIZATION_PARAM
- See Also:
- Constant Field Values
-
DOM_COMMENTS_PARAM
static final java.lang.String DOM_COMMENTS_PARAM
- See Also:
- Constant Field Values
-
DOM_DATATYPE_NORMALIZATION_PARAM
static final java.lang.String DOM_DATATYPE_NORMALIZATION_PARAM
- See Also:
- Constant Field Values
-
DOM_ELEMENT_CONTENT_WHITESPACE_PARAM
static final java.lang.String DOM_ELEMENT_CONTENT_WHITESPACE_PARAM
- See Also:
- Constant Field Values
-
DOM_ENTITIES_PARAM
static final java.lang.String DOM_ENTITIES_PARAM
- See Also:
- Constant Field Values
-
DOM_ERROR_HANDLER_PARAM
static final java.lang.String DOM_ERROR_HANDLER_PARAM
- See Also:
- Constant Field Values
-
DOM_INFOSET_PARAM
static final java.lang.String DOM_INFOSET_PARAM
- See Also:
- Constant Field Values
-
DOM_NAMESPACES_PARAM
static final java.lang.String DOM_NAMESPACES_PARAM
- See Also:
- Constant Field Values
-
DOM_NAMESPACE_DECLARATIONS_PARAM
static final java.lang.String DOM_NAMESPACE_DECLARATIONS_PARAM
- See Also:
- Constant Field Values
-
DOM_NORMALIZE_CHARACTERS_PARAM
static final java.lang.String DOM_NORMALIZE_CHARACTERS_PARAM
- See Also:
- Constant Field Values
-
DOM_SPLIT_CDATA_SECTIONS_PARAM
static final java.lang.String DOM_SPLIT_CDATA_SECTIONS_PARAM
- See Also:
- Constant Field Values
-
DOM_VALIDATE_PARAM
static final java.lang.String DOM_VALIDATE_PARAM
- See Also:
- Constant Field Values
-
DOM_VALIDATE_IF_SCHEMA_PARAM
static final java.lang.String DOM_VALIDATE_IF_SCHEMA_PARAM
- See Also:
- Constant Field Values
-
DOM_WELL_FORMED_PARAM
static final java.lang.String DOM_WELL_FORMED_PARAM
- See Also:
- Constant Field Values
-
DOM_CDATA_SECTIONS_SPLITTED_ERROR
static final java.lang.String DOM_CDATA_SECTIONS_SPLITTED_ERROR
- See Also:
- Constant Field Values
-
DOM_INVALID_CHARACTER_ERROR
static final java.lang.String DOM_INVALID_CHARACTER_ERROR
- See Also:
- Constant Field Values
-
DOM_INVALID_CHARACTER_IN_NODE_NAME_ERROR
static final java.lang.String DOM_INVALID_CHARACTER_IN_NODE_NAME_ERROR
- See Also:
- Constant Field Values
-
-
DOM_PHP_ERR
(integer)
DOM_INDEX_SIZE_ERR
(integer)
DOMSTRING_SIZE_ERR
(integer)
DOMString.
DOM_HIERARCHY_REQUEST_ERR
(integer)
DOM_WRONG_DOCUMENT_ERR
(integer)
DOM_INVALID_CHARACTER_ERR
(integer)
DOM_NO_DATA_ALLOWED_ERR
(integer)
DOM_NO_MODIFICATION_ALLOWED_ERR
(integer)
DOM_NOT_FOUND_ERR
(integer)
DOM_NOT_SUPPORTED_ERR
(integer)
DOM_INUSE_ATTRIBUTE_ERR
(integer)
DOM_INVALID_STATE_ERR
(integer)
DOM_SYNTAX_ERR
(integer)
DOM_INVALID_MODIFICATION_ERR
(integer)
DOM_NAMESPACE_ERR
(integer)
DOM_INVALID_ACCESS_ERR
(integer)
DOM_VALIDATION_ERR
(integer)
то сработает это исключение и операция не будет выполнена.
The JavaScript exception «illegal character» occurs when there is an invalid or
unexpected token that doesn’t belong at this position in the code.
Message
SyntaxError: Invalid character (Edge) SyntaxError: illegal character (Firefox) SyntaxError: Invalid or unexpected token (Chrome)
Error type
What went wrong?
There is an invalid or unexpected token that doesn’t belong at this position in the
code. Use an editor that supports syntax highlighting and carefully check your code
against mismatches like a minus sign (-
) versus a dash (–
)
or simple quotes ("
) versus non-standard quotation marks ("
).
Examples
Mismatched characters
Some characters look similar, but will cause the parser to fail interpreting your code.
Famous examples of this are quotes, the minus or semicolon
(greek question mark (U+37e) looks same).
“This looks like a string”; // SyntaxError: illegal character
// “ and ” are not " but look like it
42 – 13; // SyntaxError: illegal character
// – (en-dash) is not - but looks like it
const foo = "bar"; // SyntaxError: illegal character
// <37e> is not ; but looks like it
This should work:
"This is actually a string";
42 - 13;
const foo = "bar";
Some editors and IDEs will notify you or at least use a slightly different highlighting for it, but not all. When something like this happens to your code and you’re not able to find the source of the problem, it’s often best to just delete the problematic line and retype it.
Forgotten characters
It’s easy to forget a character here or there.
const colors = ["#000", #333", "#666"];
// SyntaxError: illegal character
Add the missing quote for "#333"
.
const colors = ["#000", "#333", "#666"];
Hidden characters
When copy pasting code from external sources, there might be invalid characters. Watch
out!
const foo = "bar";
// SyntaxError: illegal character
When inspecting this code in an editor like Vim, you can see that there is actually a
zero-width space (ZWSP) (U+200B) character.
const foo = "bar";<200b>