Javascript DOM HTML document scripts Collection get by item(index)

Introduction

Get the contents of the first <script> element (index 0) in the document:

var x = document.scripts.item(0).text;

Click the button to get the contents of the first script element in the document.

View in separate window

<!DOCTYPE html>
<html>
<body>

<script>
document.write("Hello World!");
</script>/*from  w  w  w .  ja  va  2 s  .c  o m*/
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.scripts.item(0).text;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related