Forum Moderators: open

Message Too Old, No Replies

dom core scripting

unable to window.close()

         

nyteshade

1:12 am on Sep 11, 2011 (gmt 0)

10+ Year Member



I am converting some javascript lessons from Beginning JavaScript to eliminate innerHtml and remove behaviors from my HTML to external js files according to DOM Scripting (excellent book). I'm stuck and need to do a brain-check. I have:

<div id="winobject">
<input type="button" value="Open" />
<input type="button" value="Close" />
<input type="button" value="Confirm" />
</div>
<div id="msg"></div

function prepInputElements(){
var myWindow;
if(!document.getElementsByTagName) return false;
if(!document.getElementById) return false;
if(!document.getElementById("winobject")) return false;

var mydiv = document.getElementById("winobject");
var myinputs = mydiv.getElementsByTagName("input");
var myid = document.getElementById('msg');
mytextnode = document.createTextNode("Window status.");
myid.appendChild(mytextnode);

for(var i=0; i < myinputs.length ; i++){
if(myinputs[i].getAttribute("value") == "Open"){
myinputs[i].onclick = function() {
myWindow = window.open('','','width=400,height=200');
myid.firstChild.nodeValue = "Window is open.";
}
}
if(myinputs[i].getAttribute("value") == "Close") {
myinputs[i].onclick = function() {
if (myWindow){
myWindow.close(); //<<<<<<<NOT CLOSING THE WINDOW
myid.firstChild.nodeValue = "Window is closed.";//<<BUT, THIS WORKS!
}
}
}
if(myinputs[i].getAttribute("value") == "Confirm") {
myinputs[i].onclick = function() {
if (!myWindow){
myid.firstChild.nodeValue = "Window has not been opened.";
} else {
if (myWindow.closed) {
myid.firstChild.nodeValue = "Window has been closed.";
} else {
myid.firstChild.nodeValue = "Window has not been closed.";
}
}
}
}
}
}
addLoadEvent(prepInputElements);


I'm creating the onclick behavior at document load time and everything appears to work. Even the offending line marked above gets executed, as I step through the code with firebug, but the window remains open... well, I just tried it with Opera and it works! Works with Chrome too! Hmmm, must be a FF problem? Anyone?

nyteshade

1:24 am on Sep 11, 2011 (gmt 0)

10+ Year Member



Hmmm, I restarted XP and FF is working with it just fine, my bad. I even did a hard reload and restarted FF but it didn't work until I restarted XP?