This is one of the most problematic issues I have encountered in working
with JavaScript. The following code demonstrates the kind of difficulty
I am having:
/* code */
var newSrc = "<H1>hello</H1>";
var w = window.open("javascript:opener.newSrc","w",
"height=300,width=400,scrollbars=yes");
var myDoc = w.document;
myDoc.writeln("<H1>world</H1>");
/* end code */
When I load the page the first time I get 'world'. The first reload
displays 'hello'. Thereafter they toggle. Such behavior makes it very
difficult to work with windows and window.documents in a deterministic way.
The next piece of code shows another inelegant approach to this
situation. If you try this you will notice that, by commenting out the
setTimeout(), and uncommenting the w.document.writeln(...) a different
result is obtained. In the shown case the text is displayed upon load
and every subsequent reload. In the modified case the text is not
displayed on odd numbered reloads.
/* code */
w = window.open("about:blank", "w","height=300, width=400, scrollbars=yes");
GOTO = "w.document.writeln('<H1>With setTimeout</H1>')";
setTimeout(GOTO, 0);
// w.document.writeln('<H1>No setTimeout</H1>')
/* end code */
As my choice of variable names indicates the setTimeout is a
non-returning branch statement. Am I the only person who sees a problem
with this situation? To me this is a major issue. I'd like to be able
to do something similar to:
myDoc = null;
var str = "<H1>Hello World</H1>";
if (myDoc) {
myDoc.writeln(str);
} else {
w = window.open("about:blank"
,"mainWin","height=300, width=400, scrollbars=yes");
myDoc = w.document;
myDoc.writeln(str);
}
Unfortunately this does not produce the desired/expected result.
Opinions?
Steven