Get the contents of the first <script> element (index 0) in the document:
var x = document.scripts[0].text;
<!DOCTYPE html> <html> <body> <script> document.write("Hello World!"); </script>/*from w w w . j a v a 2s. co m*/ <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() { var x = document.scripts[0].text; document.getElementById("demo").innerHTML = x; } </script> </body> </html>