If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Your code works fine for me as long as you make sure the body element exists first. Wrap your code in a function and call it with document.addEventListener("DOMContentLoaded", function_name) or place the script element within the body element as jmrker did.

Originally Posted by Lucas Azevedo

and i don't like to use s.appendChild(document.createTextNode(code));

To append script element text without using node methods, you can use the script element-specific text property, the HTML-specific innerHTML property, or the generic textContent property. Examples:

If you are going to add a script tag then put the code it is to contain in a separate file and attach that as the src attribute.

If you have the code actually in the script already then simply define it as a Function and then execute it. No need for eval.

that depends on the code, if globals/locals are being jump-started into life, then Function won't work, while eval will. adding a new external script intrinsically calls eval, so there's nothing wrong about using it do do the same thing without http traffic...

Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

that depends on the code, if globals/locals are being jump-started into life, then Function won't work, while eval will. adding a new external script intrinsically calls eval, so there's nothing wrong about using it do do the same thing without http traffic...

You can't use globals or any variables defined elsewhere when you use eval either unless you identify that you are using antiquated rather than current JavaScript by omitting the "use strict"; statement