opener.openerThat is indeed a valid reference to the main parent window. But what if the main parent window or the 1st Sub has been closed? You'd think that by testing the closed property of the parent window would keep any errors from being generated:
if(!opener.opener.closed) { // if opener.opener.closed is falseNow we've got a problem and it is this: sub windows do not have a parent-child relationship like framesets do with their frame source documents. What this means to us is that if either the Main parent window or 1st Sub window is closed, there is no longer any connection between the 2nd Sub and the original Main window. The above statement will cause an error because as far as the JavaScript engine is concerned, opener.opener no longer exists when either the main or 1st Sub window has been closed.
....
}
var mainWin = opener.opener // store reference to main window in global variableThe same technique can be applied to a main window that happens to be a frameset document, as long as it is referenced correctly:
function writeToMain(){
if(!mainWin.closed){
mainWin.document.write("I'm still open, so you can write to me")
mainWin.document.close()
// must "close the stream" after writing to NN windows if page has loaded (different than window.close())
}
else{
alert("Sorry, the main has been closed")
}
}
var mainWin = opener.opener.topFor more on frames, see the frames tutorial.