Mozilla onload: "no properties"; IE OK

K

kj

This problem is driving me nuts. The code at the end of this post
below works fine with IE, but fails with Mozilla. You can see it
in action at

http://tinyurl.com/2jvo3

With Mozilla 1.4 and 1.6, the function msg works fine if it's
installed as an onclick handler for the button, but fails as an
onload handler for the page. The error is "console has no properties",
and is triggered by the line.

console.document.open("text/plain");

The code works fine on all versions of IE I've tested it on.

Is there an error in my code (that IE is letting slide), or is this
a bug in Mozilla? If the latter, is there a workaround?

Thanks!

kj



<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN"
"http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">

<head><title>JAVASCRIPT STUMPER</title></head>


<body onload="javascript:msg('PLEASE HELP ME!!!') // bombs on NS, moz">

<script type="text/javascript">

// Adapted from Flanagan's "JavaScript: The Definitive Guide", 3rd Ed.

var console = null;
var litany = "";
var times = 0;

function msg(s) {
var MHUAHAHA = "";
if ((console == null) || (console.closed)) {
console = window.open("","console","width=600,height=300,resizable");
console.document.open("text/plain");
MHUAHAHA = litany;
}

MHUAHAHA += (++times > 1
? "I've told you " + times + " times already!!! "
: "") + "RESISTANCE IS FUTILE!\n";

console.document.write(MHUAHAHA);
litany = MHUAHAHA;
}

</script>
<h1>ALL YOUR BASE ARE BELONG TO US!</h1>
<form>
<input type=button value="HELP!!!" onclick="javascript:msg(this.value)"/>
</form>
</body>
</html>
 
G

Grant Wagner

kj said:
This problem is driving me nuts. The code at the end of this post
below works fine with IE, but fails with Mozilla. You can see it
in action at

http://tinyurl.com/2jvo3

With Mozilla 1.4 and 1.6, the function msg works fine if it's
installed as an onclick handler for the button, but fails as an
onload handler for the page. The error is "console has no properties",
and is triggered by the line.

console.document.open("text/plain");

The code works fine on all versions of IE I've tested it on.

Is there an error in my code (that IE is letting slide), or is this
a bug in Mozilla? If the latter, is there a workaround?

Thanks!

kj

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN"
"http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">

<head><title>JAVASCRIPT STUMPER</title></head>

<body onload="javascript:msg('PLEASE HELP ME!!!') // bombs on NS, moz">

<script type="text/javascript">

// Adapted from Flanagan's "JavaScript: The Definitive Guide", 3rd Ed.

var console = null;
var litany = "";
var times = 0;

function msg(s) {
var MHUAHAHA = "";
if ((console == null) || (console.closed)) {
console = window.open("","console","width=600,height=300,resizable");

This is an asynchronous method call. window.open() is called, opens a new
window and returns a reference to the newly created window. There is no
guarantee, none, that when the method window.open() returns, there is a valid
document in the newly created window.
console.document.open("text/plain");

You immediately attempt to access the document property of the newly opened
window, as stated above, there is no guarantee that a document property of a
newly opened window is available to do _anything_ with immediately following
a window.open() call.
MHUAHAHA = litany;
}

MHUAHAHA += (++times > 1
? "I've told you " + times + " times already!!! "
: "") + "RESISTANCE IS FUTILE!\n";

console.document.write(MHUAHAHA);
litany = MHUAHAHA;
}

</script>
<h1>ALL YOUR BASE ARE BELONG TO US!</h1>
<form>
<input type=button value="HELP!!!" onclick="javascript:msg(this.value)"/>
</form>
</body>
</html>

The "fix" or "workaround" is to actually load a .html file into the newly
created window that contains something like:

<body onload="if (opener && opener.callBack) opener.callBack();">

callBack(); would be a function in the opener window that writes whatever
content you want to the newly created window (or does whatever else you would
like done). In this way, you can be assured that the only time the code to
write content to the new window runs is once the new window is opened and
ready for it's content.

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
L

Lee

kj said:
This problem is driving me nuts. The code at the end of this post
below works fine with IE, but fails with Mozilla. You can see it
in action at

http://tinyurl.com/2jvo3

With Mozilla 1.4 and 1.6, the function msg works fine if it's
installed as an onclick handler for the button, but fails as an
onload handler for the page. The error is "console has no properties",
and is triggered by the line.

Mozilla is blocking popups. A window.open() call from
the onload handler is considered to be a popup that
should be blocked. You can change that setting.
 
D

DU

Grant said:
kj wrote:




This is an asynchronous method call. window.open() is called, opens a new
window and returns a reference to the newly created window. There is no
guarantee, none, that when the method window.open() returns, there is a valid
document in the newly created window.




You immediately attempt to access the document property of the newly opened
window, as stated above, there is no guarantee that a document property of a
newly opened window is available to do _anything_ with immediately following
a window.open() call.

Correct. He also calls, uses a parameter which is no longer valid in DOM
2 HTML. open() takes no parameters.

http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-72161170


He never closes the document stream: so the browser waits (Mozilla-based
browsers are more sensitive to this) for more info and keep the stream
open. It gives the user the impression that the document never ends
loading, the progress icon never stops turning, progressmeter is always
active, as if the browser is always waiting for more info.

"close
Closes a document stream opened by open() and forces rendering."

http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-98948567
The "fix" or "workaround" is to actually load a .html file into the newly
created window that contains something like:

<body onload="if (opener && opener.callBack) opener.callBack();">

callBack(); would be a function in the opener window that writes whatever
content you want to the newly created window (or does whatever else you would
like done). In this way, you can be assured that the only time the code to
write content to the new window runs is once the new window is opened and
ready for it's content.

I entirely agree with your explanations and solution. I think this
problem is so often encountered and is not well documented that it
should be addressed by this newsgroup FAQ.
This happened to me once and I couldn't find any documentation on this
issue.

DU
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top