Mozilla onload: "no properties"; IE OK

Discussion in 'Javascript' started by kj, Jul 2, 2004.

  1. kj

    kj Guest

    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>
    --
    NOTE: In my address everything before the period is backwards.
     
    kj, Jul 2, 2004
    #1
    1. Advertising

  2. kj

    Grant Wagner Guest

    kj wrote:

    > 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 <>

    * 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
     
    Grant Wagner, Jul 2, 2004
    #2
    1. Advertising

  3. kj

    Lee Guest

    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.
     
    Lee, Jul 2, 2004
    #3
  4. kj

    DU Guest

    Grant Wagner wrote:
    > kj wrote:
    >
    >
    >>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.
    >


    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


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


    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

    >>}
    >>
    >></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.
    >


    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

    > --
    > | Grant Wagner <>
    >
    > * 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
    >
    >
     
    DU, Jul 3, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fabio R.
    Replies:
    0
    Views:
    801
    Fabio R.
    Mar 25, 2005
  2. Starry Gordon
    Replies:
    1
    Views:
    112
  3. David Otton

    window.onload and body.onload differences

    David Otton, Nov 4, 2004, in forum: Javascript
    Replies:
    2
    Views:
    558
    Martin Honnen
    Nov 4, 2004
  4. Replies:
    5
    Views:
    262
    Thomas 'PointedEars' Lahn
    May 15, 2005
  5. marco

    onload -->* no onload

    marco, Jun 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    206
    marco
    Jun 24, 2006
Loading...

Share This Page