Your script is executed before the DOM is ready, so getting the <body> is a race condition. I really get the same error in Chrome 15 and Firefox 8.
You can see the works code when called after loading the page, for example, in a function
HTML
<a href="#" onclick="return append()">append</a>
Javascript
function append() { var scriptContent = "var whatever=1"; var _js = document.createElement('script'); _js.setAttribute('type', 'text/javascript'); textNode = document.createTextNode(scriptContent); _js.appendChild(textNode); document.getElementsByTagName('body')[0].appendChild(_js); return false; }
source share