Jump to content

Cannot read property 'document' ??


Recommended Posts

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>

 

Link to post
Share on other sites

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);

 

Link to post
Share on other sites

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

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...