Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.createElement illegal invocation

I was thinking to minimize some javascript code but I get this "illegal invocation" error when I try to call a function through an alias

var d = document.createElement;
d('input');

Does anybody know why? tx

like image 636
Elfy Avatar asked Dec 11 '22 21:12

Elfy


1 Answers

Looks like this has been addressed by others. It boils down to the fact that

document.createElement checks to make sure that this refers to document. You can bypass this behavior by doing the following:

Either A: always use it as document.createElement(tagname) OR

B

var o = document.createElement
o.call(document, tagname)

C

var d = document.createElement.bind(document); 

(from above answer)

See http://blog.vjeux.com/2011/javascript/hook-document-createelement.html

like image 140
CollinD Avatar answered Dec 26 '22 23:12

CollinD