Get the contents of the <script> element with id="myScript"
in the document:
var x = document.scripts.namedItem("myScript").text;
Click the button to get the contents of the script element with id="myScript"
.
<!DOCTYPE html> <html> <body> <script> document.write("Hello World!"); </script>/*from w ww. ja va 2 s .c o m*/ <button onclick="myFunction()">Test</button> <p id="demo"></p> <script id="myScript"> function myFunction() { var x = document.scripts.namedItem("myScript").text; document.getElementById("demo").innerHTML = x; } </script> </body> </html>