With this approach, you can target an item by ID and insert whatever you want into it, but the solution proposed by Paul S is simpler and cleaner.
<html> <head> <script type="text/javascript"> var myVar = 42; function displayMyVar(targetElementId) { document.getElementById(targetElementId).innerHTML = myVar; } </script> </head> <body onload="displayMyVar('target');"> <span id="target"></span> </body> </html>
source share