Javascript DOM HTML Script Object create

Introduction

We can create a <script> element via the document.createElement() method:

var x = document.createElement("SCRIPT");

Click the button to create a SCRIPT element that writes "Hello World!" to console.

View in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {//from   w  ww .j a v  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>



PreviousNext

Related