I am trying to use windows.load without a global variable.
HTML code:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>Test</title> <link rel="stylesheet" href="main.css"> <script type="text/javascript" src="script.js"></script> </head> <body> <form> Name: <input type="text" id="txt1"><br><br> <input type="button" value="Check Input" id="b1"> </form> </body> </html>
JavaScript global variable code:
var myButton; window.onload = function () { "use strict"; myButton = document.getElementById("b1"); myButton.addEventListener("click",alertMM); }; function alertMM() { "use strict"; window.console.log(myButton.value); }
And finally, NOT WORKING without the global variable code:
var myNS = { myButton: undefined,
The reason I want to stop using the global variable is because I'm afraid that it will conflict with future code.
Thank you in advance
Adrian
source share