Get the contents of the <script> element:
var x = document.getElementById("myScript").text
Click the button to return the contents of the script element.
<!DOCTYPE html> <html> <body> <script id="myScript"> document.write("Hello World!"); </script>//from w w w. j av a2 s .c o m <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() { var x = document.getElementById("myScript").text; document.getElementById("demo").innerHTML = x; } </script> </body> </html>
The text property sets or gets the contents of the <script> element.
The text property accepts and returns a String type value.