Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will document.createElement("") ever fail?

Please relocate this if I am asking on the wrong site (Javascript based question, so I'm pretty sure it's in the right place).

I am currently dealing with error catching on a project that is soon to be completed and set up for the client. I am wondering whether the following line in Javascript will ever fail?

var element = document.createElement("someelement")

Provided that:

  • someelement is a valid HTML tag (div, a, h1, etc)
  • element can be any string that is not a reserved word and starts with a letter.
  • The device it is running on is a modern device and RAM is not a problem <- EDIT

Will it ever fail? And if it does, what will it return?

NOTE: Sources would be great if you have them.

like image 495
JosephGarrone Avatar asked Oct 19 '13 10:10

JosephGarrone


3 Answers

http://www.w3.org/TR/DOM-Level-2-Core/core.html

 Element            createElement(in DOMString tagName)
                                        raises(DOMException);

And

Exceptions
DOMException

INVALID_CHARACTER_ERR: Raised if the specified name contains an illegal character.

Invalid character is e.g. space:

document.createElement(" ")

http://jsfiddle.net/yRY6Q/

like image 55
Esailija Avatar answered Nov 20 '22 07:11

Esailija


According to the spec, a DOMException is raised if tagName contains invalid characters (whitespace characters, etc.).

In Firefox, document.creatElement will only fail when invalid characters are passed. Null or undefined is OK.

In Chrome, document.createElement will fail and throw a InvalidCharacterError when passing null or invalid characters. Undefined is OK.

As some users pointed out in the comments, it can always fail due to memory resources etc. but you cannot do anything to prevent that.

So, if someelement is a valid HTML tag (div, a, h1, etc), it will never fail.

You can use this snippet to find invalid characters:

for(var i = 0; i < 200; i++) {
    var chr = String.fromCharCode(i);
    var msg = i + ". " + chr + ": ";

    try {
        var elm = document.createElement(chr);
        msg += "OK";
    }
    catch(e)
    {
        msg += "FAIL";
    }

    console.log(msg);
}
like image 29
alexn Avatar answered Nov 20 '22 05:11

alexn


The only exception I am aware of is the following (Note that I'm using Chrome):

Error: InvalidCharacterError: DOM Exception 5

This exception arises when you pass null, an object or an array as the parameter. It also fails when you pass a string with invalid HTML characters, such as @!#?.

If you pass undefined, or do not specify the parameter, it will create an undefined element.

<undefined></undefined>

If you are sure that this function is throwing an error, you should probably be checking whether the string you are passing is !== null before calling createElement, as that is likely the source of your problem.

If the error still occurs, perhaps check that the string consists of only letters a-z, the underscore and the hyphen.

like image 1
azz Avatar answered Nov 20 '22 05:11

azz