vmars316 5 Posted April 10, 2017 Report Share Posted April 10, 2017 Hello & Thanks, I am getting error msg: jsMsgBoxPlusTinker.html:13 Uncaught TypeError: Cannot read property 'document' of null at myFunction And I can't see my error, need help from someone with better eyes than me, Thanks: <!DOCTYPE html> <html> <title>file:///C:/vmars.us/javascript/SimpleGame-js/ClaustrophobiaGame/jsMsgBoxPlusTinker.html </title> <body onload = "myFunction()"> <p>Click Button to open a msgBox</p> <button onclick="myFunction()">msgBox</button> <script type = "text/javascript" > var myWindowHtml = '<p>Yes , No , Continue , Cancel ?</p><p><button id="yesBtn">Yes</button><button id="noBtn">No</button><button id="continueBtn">Continue</button><button id="cancelBtn">Cancel</button></p>'; var myWindow; var btn1 = "yesBtn"; var btn2 = "noBtn"; var btn3 = "continueBtn"; var btn4 = "cancelBtn"; function myFunction() { myWindow = window.open("", "MsgWindow", "width=400,height=100"); myWindow.document.write(myWindowHtml); myWindow.document.getElementById(btn1).addEventListener("click", function(){yesClick();}); myWindow.document.getElementById(btn2).addEventListener("click", function(){noClick();}); myWindow.document.getElementById(btn3).addEventListener("click", function(){continueClick();}); myWindow.document.getElementById(btn4).addEventListener("click", function(){cancelClick();}); } function yesClick(){ alert("you Clicked Yes !\n Bye..."); myWindow.close(); } function noClick(){ alert("you Clicked no !\n Bye..."); myWindow.close(); } function continueClick(){ alert("you Clicked continue !\n Bye..."); myWindow.close(); } function cancelClick(){ alert("you Clicked cancel !\n Bye..."); myWindow.close(); } </script> </body> </html> Quote Link to post Share on other sites
Ingolme 1,034 Posted April 10, 2017 Report Share Posted April 10, 2017 If the window was stopped by a popup blocker that error will occur. For security reasons, most browsers will block a popup that wasn't created by a click or key event. The popup from your onload event will have that problem. Remove the onload event and just use the button. For efficiency, don't wrap event handler functions in function wrappers, they're not necessary. // Your current code: addEventListener("click", function(){yesClick();}); // Without function wrapper addEventListener("click", yesClick); Quote Link to post Share on other sites
vmars316 5 Posted April 10, 2017 Author Report Share Posted April 10, 2017 Thanks Ingolme , I plan to delete the "<body onload = "myFunction()">", and the "<button onclick="myFunction()">msgBox</button>" code. And I want to call "myFunction" from the js code. Is there a way to get pure js to call "myFunction" from within the same page? Thanks Quote Link to post Share on other sites
Ingolme 1,034 Posted April 10, 2017 Report Share Posted April 10, 2017 A pop-up window won't work if it's not called by a click event or keyboard event. Quote Link to post Share on other sites
vmars316 5 Posted April 10, 2017 Author Report Share Posted April 10, 2017 Aw shucks , Thanks Quote Link to post Share on other sites
Ingolme 1,034 Posted April 10, 2017 Report Share Posted April 10, 2017 You don't need a new window, you could create a modal box instead. W3Schools even has a tutorial for that https://www.w3schools.com/howto/howto_css_modals.asp Quote Link to post Share on other sites
vmars316 5 Posted April 11, 2017 Author Report Share Posted April 11, 2017 Thanks, I am trying to send a msg (popup window) without a Button. Guess I'll have to do this: document.getElementById("currentLevelLbl").innerHTML = currentLevelLbl + startClockAt ; or maybe a popup-image. Thanks Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.