Jump to content

Cannot read property 'document' ??


vmars316

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 comment
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 comment
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 comment
Share on other sites

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 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...