it keeps saying variable null though assigned button in html. doesn't allow button press work. variable "clickme"
var yourname; //global variable accessible functions function showanothermessage() { alert("hi " + yourname + ".\nthis alert message no longer defined\nin html in javascript file"); } function init() { yourname = prompt("hi. enter name.\nwhen browser window first loaded\nthe function containing prompt window called.", "your name"); var clickme = document.getelementbyid("buttonclick"); clickme.onclick = showanothermessage; } window.onload = init();
avoid window.onload because it's fired late in page lifecycle - it's fired after everything loaded, including images - can several seconds after page has loaded.
instead, use dom events api, , use domcontentloaded event:
window.addeventlistener('domcontentloaded', function(e) { // page startup code goes here } ); another reason avoid window.onload = ... overwrite previous event-handler onload event, whereas addeventlistener not remove previous event handler.
Comments
Post a Comment