Create a Script Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Script

Introduction

You can create a <script> element by using the document.createElement() method:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">create a SCRIPT element</button>

<script>
function myFunction() {/*from   w  w  w .j  av a  2 s  .  c o  m*/
    var x = document.createElement("SCRIPT");
    var t = document.createTextNode("console.log('Hello World!')");
    x.appendChild(t);
    document.body.appendChild(x);
}
</script>

</body>
</html>

Related Tutorials