To make a variable visible in all areas, you must declare it in the global area itself:
<script> var variableName; </script>
or you can bind it to a global context (window):
window['variableName'] = value;
Your code does not work because when x is defined, the form is unavailable, which means that you are not assigning anything to the variable.
You must transfer your initialization to the event handler for the onload event:
window.onload = function(){ window.x = document.myForm.myInput; };
or
var x; window.onload = function(){ x = document.myForm.myInput; };
source share