setPage is not defined error in Mozilla/Firefox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Johan

    setPage is not defined error in Mozilla/Firefox

    Hi,

    I've been working on the following piece of code and it works fine in IE
    and Opera, dut keep getting the same error in Firefox. Can anyone tell
    me what's wrong with this code? What I'm trying to do is rewrite the
    left (navigationfram e) and load a page on the right (mainframe).

    tia,
    Johan

    Here's a piece of the code:

    function setHome(pageCod e) {
    with (document) {
    open();
    write('<?xml version="1.0" encoding="iso-8859-1"?>\n<!DOCT YPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">\n');
    write('\n<html xmlns="http://www.w3.org/1999/xhtml">\n\n');
    write('<head>\n <link rel="stylesheet " type="text/css"
    href="./nav.css" />\n<script src="./nav.js" type="text/javascript"
    language="JavaS cript">\n</script>\n</head>\n\n');
    write('<body>\n ');
    write('<div id="navigation" >\n');
    write('<table>\ n');
    write('<tr>\n<t d><a href="#" onclick="setHom e(\'./homemain.html\' )"
    class="current" >Home</a></td>\n</tr>');
    write('<tr>\n<t d><a href="#"
    onclick="setSof tware(\'./software.html\' )">Software</a></td>\n</tr>');
    write('<tr>\n<t d><a href="#"
    onclick="setLin ks(\'linksmain. html\')">Links</a></td>\n</tr>');
    write('<tr>\n<t d><a href="#"
    onclick="setInf o(\'./informatie.html \')">Informatie </a></td>\n</tr>');
    write('</table>');
    write('</div>');
    write('</body>');
    write('</html>');
    close();
    setPage(pageCod e);
    }
    }

    ....

    function setPage(url) {
    parent.mainfram e.location.href = url.toString();
    }
  • Andrew Thompson

    #2
    Re: setPage is not defined error in Mozilla/Firefox

    On Tue, 07 Sep 2004 22:18:16 +0200, Johan wrote:
    [color=blue]
    > I've been working on the following piece of code and it works fine in IE
    > and Opera, dut keep getting the same error in Firefox.[/color]

    So, let me guess.. That's a 'Fluffy_kittens
    not defined error' perhaps? In that case,
    change line 43, char 19 from a '.' to a ';'.

    No good?

    Filing that it might be more productive
    to state *specifically* what error you
    are getting.
    [color=blue]
    > Here's a piece of the code:[/color]

    Aaahh yes. 'a piece of the code'..

    If it it is broken, and you do not know
    how to fix it, what makes you think it
    is this part of the code that is broken?

    <http://www.physci.org/codes/sscce.jsp>

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology

    Comment

    • max power

      #3
      Re: setPage is not defined error in Mozilla/Firefox

      In article <413e1787$0$767 $3a628fcd@reade r10.nntp.hccnet .nl>, Johan
      <johankeesAT@il seDOT.nl> wrote:
      [color=blue]
      > Here's a piece of the code:
      >
      > function setHome(pageCod e) {
      > with (document) {
      > open();[/color]
      <snip>[color=blue]
      > close();
      > setPage(pageCod e);
      > }
      > }
      >
      > ...
      >
      > function setPage(url) {
      > parent.mainfram e.location.href = url.toString();
      > }[/color]

      I'm no real JS guru, but if it's like other languages, then you have to
      define functions *before* you can call/use them. Your code calls
      setPage() inside the setHome() function, but setPage() is not defined
      until later in the code - I'm surprised it works in any browser.

      Comment

      • Michael Winter

        #4
        Re: setPage is not defined error in Mozilla/Firefox

        On Tue, 07 Sep 2004 22:18:16 +0200, Johan <johankeesAT@il seDOT.nl> wrote:
        [color=blue]
        > I've been working on the following piece of code and it works fine in IE
        > and Opera, dut keep getting the same error in Firefox. Can anyone tell
        > me what's wrong with this code? What I'm trying to do is rewrite the
        > left (navigationfram e) and load a page on the right (mainframe).[/color]

        [snipped code]

        Could you provide a URL? I don't see anything wrong (though I might have
        missed something). A working example would make it much easier to see the
        problem.

        I would like to mention a few points before I finish:

        - You should make sure you escape "</" sequences into "<\/". This is
        because browsers are allowed to treat "</" as the end of the SCRIPT
        element.
        - The TITLE element is required in a HTML document. Even if you leave it
        empty[1],

        <title></title>

        always include it.
        - You can use the writeln method to end a written line with a new-line
        character.
        - The language attribute in a SCRIPT element has been deprecated in favour
        of the (required) type attribute. You needn't use the former any more.
        - As the content you're writing into the frame is static, why not load a
        HTML file instead?

        Mike



        [1] There's no real reason to leave it empty. You'll always have some form
        of label for a page.

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: setPage is not defined error in Mozilla/Firefox

          max power <maxpower@inval id.com> writes:
          [color=blue]
          > I'm no real JS guru, but if it's like other languages, then you have to
          > define functions *before* you can call/use them.[/color]

          It is defined before it is used. Both functions are defined at the
          same scope level, and therefore at almost the same time, long before
          the code in that scope is executed.
          [color=blue]
          > Your code calls setPage() inside the setHome() function, but
          > setPage() is not defined until later in the code[/color]

          The position in the code is not important. The use has to be later (in
          time) than the declaration, but not after in the code.
          [color=blue]
          > - I'm surprised it works in any browser.[/color]

          I think the problem is a string literal that spans several
          lines. That's not valid Javascript. A string literal may not
          contain newlines (but can contain the escape sequence "\n" which
          will cause the resulting string value to contain a newline).

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Johan

            #6
            Re: setPage is not defined error in Mozilla/Firefox

            max power wrote:[color=blue]
            > In article <413e1787$0$767 $3a628fcd@reade r10.nntp.hccnet .nl>, Johan
            > <johankeesAT@il seDOT.nl> wrote:
            >
            >[color=green]
            >>Here's a piece of the code:
            >>
            >>function setHome(pageCod e) {
            >> with (document) {
            >> open();[/color]
            >
            > <snip>
            >[color=green]
            >> close();
            >> setPage(pageCod e);
            >> }
            >>}
            >>
            >>...
            >>
            >>function setPage(url) {
            >> parent.mainfram e.location.href = url.toString();
            >>}[/color]
            >
            >
            > I'm no real JS guru, but if it's like other languages, then you have to
            > define functions *before* you can call/use them. Your code calls
            > setPage() inside the setHome() function, but setPage() is not defined
            > until later in the code - I'm surprised it works in any browser.[/color]
            Thanks for your reply. I 've tried what you're suggesting, but it didn't
            change a thing.
            Johan

            Comment

            • Johan

              #7
              Re: setPage is not defined error in Mozilla/Firefox

              Lasse Reichstein Nielsen wrote:
              [color=blue]
              > max power <maxpower@inval id.com> writes:
              >
              >[color=green]
              >>I'm no real JS guru, but if it's like other languages, then you have to
              >>define functions *before* you can call/use them.[/color]
              >
              >
              > It is defined before it is used. Both functions are defined at the
              > same scope level, and therefore at almost the same time, long before
              > the code in that scope is executed.
              >
              >[color=green]
              >>Your code calls setPage() inside the setHome() function, but
              >>setPage() is not defined until later in the code[/color]
              >
              >
              > The position in the code is not important. The use has to be later (in
              > time) than the declaration, but not after in the code.
              >
              >[color=green]
              >>- I'm surprised it works in any browser.[/color]
              >
              >
              > I think the problem is a string literal that spans several
              > lines. That's not valid Javascript. A string literal may not
              > contain newlines (but can contain the escape sequence "\n" which
              > will cause the resulting string value to contain a newline).
              >
              > /L[/color]
              Thanks for your reply. As far as I can see I'm doing what you're
              suggesting. I don't see where the newlines are, because every write
              starts on a newline and spans just one line.
              Johan

              Comment

              • Johan

                #8
                Re: setPage is not defined error in Mozilla/Firefox

                Michael Winter wrote:
                [color=blue]
                > On Tue, 07 Sep 2004 22:18:16 +0200, Johan <johankeesAT@il seDOT.nl> wrote:
                >[color=green]
                >> I've been working on the following piece of code and it works fine in
                >> IE and Opera, dut keep getting the same error in Firefox. Can anyone
                >> tell me what's wrong with this code? What I'm trying to do is rewrite
                >> the left (navigationfram e) and load a page on the right (mainframe).[/color]
                >
                >
                > [snipped code]
                >
                > Could you provide a URL? I don't see anything wrong (though I might
                > have missed something). A working example would make it much easier to
                > see the problem.
                >
                > I would like to mention a few points before I finish:
                >
                > - You should make sure you escape "</" sequences into "<\/". This is
                > because browsers are allowed to treat "</" as the end of the SCRIPT
                > element.
                > - The TITLE element is required in a HTML document. Even if you leave
                > it empty[1],
                >
                > <title></title>
                >
                > always include it.
                > - You can use the writeln method to end a written line with a new-line
                > character.
                > - The language attribute in a SCRIPT element has been deprecated in
                > favour of the (required) type attribute. You needn't use the former any
                > more.
                > - As the content you're writing into the frame is static, why not load
                > a HTML file instead?
                >
                > Mike
                >
                >
                >
                > [1] There's no real reason to leave it empty. You'll always have some
                > form of label for a page.
                >[/color]
                Thanks for your reply. I changed the </ into <\/ and removed the
                language bit, added a title and changed write into writeln and removed
                the \n bits. This didn't solve it :(

                I'm not using different html files because I would be changing to many
                filis if I add or remove an menuitem. This way I'm only working on one
                file. I will be changing the code into something more OO but for now
                it's ok if it just would work.

                I don't have a link to the code btw, sorry. The rest of the code is
                build the same as the first part (as the setHome part). It is used to
                show or hide several menuitems. There're no other functions in the file
                (it is an .js file)

                Here's the changed code, maybe it becomes more clear whats wrong(?):

                function setPage(url) {
                parent.mainfram e.location.href = url.toString();
                }

                function setHome(pageCod e) {
                with (document) {
                open();
                writeln('<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYP E html
                PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">');
                writeln('<html xmlns="http://www.w3.org/1999/xhtml">');
                writeln('<head> <link rel="stylesheet " type="text/css"
                href="./nav.css" /><title>Beloi s Automatisering -
                Software<\/title><script src="./nav.js"
                type="text/javascript"><\/script><\/head>');
                writeln('<body> ');
                writeln('<div id="navigation" >');
                writeln('<table >');
                writeln('<tr><t d><a href="#" onclick="setHom e(\'./homemain.html\' )"
                class="current" >Home<\/a><\/td><\/tr>');
                writeln('<tr><t d><a href="#"
                onclick="setSof tware(\'./software.html\' )">Software< \/a><\/td><\/tr>');
                writeln('<tr><t d><a href="#"
                onclick="setLin ks(\'linksmain. html\')">Links< \/a><\/td><\/tr>');
                writeln('<tr><t d><a href="#"
                onclick="setInf o(\'./informatie.html \')">Informatie <\/a><\/td><\/tr>');
                writeln('<\/table>');
                writeln('<\/div>');
                writeln('<\/body>');
                writeln('<\/html>');
                close();
                setPage(pageCod e);
                }
                }
                ....

                Comment

                • Johan

                  #9
                  Re: setPage is not defined error in Mozilla/Firefox

                  Andrew Thompson wrote:
                  [color=blue]
                  > On Tue, 07 Sep 2004 22:18:16 +0200, Johan wrote:
                  >
                  >[color=green]
                  >>I've been working on the following piece of code and it works fine in IE
                  >>and Opera, dut keep getting the same error in Firefox.[/color]
                  >
                  >
                  > So, let me guess.. That's a 'Fluffy_kittens
                  > not defined error' perhaps? In that case,
                  > change line 43, char 19 from a '.' to a ';'.
                  >
                  > No good?
                  >
                  > Filing that it might be more productive
                  > to state *specifically* what error you
                  > are getting.[/color]
                  As it says in the subject "setPage is not defined". It is shown in the
                  Javascript console.[color=blue]
                  >
                  >[color=green]
                  >>Here's a piece of the code:[/color]
                  >
                  >
                  > Aaahh yes. 'a piece of the code'..
                  >
                  > If it it is broken, and you do not know
                  > how to fix it, what makes you think it
                  > is this part of the code that is broken?
                  >
                  > <http://www.physci.org/codes/sscce.jsp>
                  >[/color]
                  The rest of the code is build the same way.

                  Comment

                  • Lasse Reichstein Nielsen

                    #10
                    Re: setPage is not defined error in Mozilla/Firefox

                    Johan <johankeesAT@il seDOT.nl> writes:
                    [color=blue]
                    > Thanks for your reply. As far as I can see I'm doing what you're
                    > suggesting. I don't see where the newlines are, because every write
                    > starts on a newline and spans just one line.[/color]

                    I cut this from another reply:
                    ---
                    writeln('<tr><t d><a href="#"
                    onclick="setSof tware(\'./software.html\' )">Software< \/a><\/td><\/tr>');
                    ---
                    The way "onclick" is indented suggests that it is on another line,
                    and it's not just your news-client that has broken the line.

                    I can't be sure ofcourse. Some news-clients always breaks long lines
                    (both at the posting and the reading end), and they aren't consistent
                    about how they do it, which is why program code posted here should not
                    be too long (72 characters works well).

                    What you posted does have incorrect newlines inside the string
                    literal. That bug, if it is in the original, would give the symptoms
                    you are experiencing.

                    /L
                    --
                    Lasse Reichstein Nielsen - lrn@hotpop.com
                    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                    'Faith without judgement merely degrades the spirit divine.'

                    Comment

                    • Grant Wagner

                      #11
                      Re: setPage is not defined error in Mozilla/Firefox

                      Johan wrote:
                      [color=blue]
                      > Hi,
                      >
                      > I've been working on the following piece of code and it works fine in IE
                      > and Opera, dut keep getting the same error in Firefox. Can anyone tell
                      > me what's wrong with this code? What I'm trying to do is rewrite the
                      > left (navigationfram e) and load a page on the right (mainframe).
                      >
                      > tia,
                      > Johan
                      >
                      > Here's a piece of the code:
                      >
                      > function setHome(pageCod e) {
                      > with (document) {
                      > open();
                      > write('<?xml version="1.0" encoding="iso-8859-1"?>\n<!DOCT YPE html
                      > PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                      > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">\n');
                      > write('\n<html xmlns="http://www.w3.org/1999/xhtml">\n\n');
                      > write('<head>\n <link rel="stylesheet " type="text/css"
                      > href="./nav.css" />\n<script src="./nav.js" type="text/javascript"
                      > language="JavaS cript">\n</script>\n</head>\n\n');
                      > write('<body>\n ');
                      > write('<div id="navigation" >\n');
                      > write('<table>\ n');
                      > write('<tr>\n<t d><a href="#" onclick="setHom e(\'./homemain.html\' )"
                      > class="current" >Home</a></td>\n</tr>');
                      > write('<tr>\n<t d><a href="#"
                      > onclick="setSof tware(\'./software.html\' )">Software</a></td>\n</tr>');
                      > write('<tr>\n<t d><a href="#"
                      > onclick="setLin ks(\'linksmain. html\')">Links</a></td>\n</tr>');
                      > write('<tr>\n<t d><a href="#"
                      > onclick="setInf o(\'./informatie.html \')">Informatie </a></td>\n</tr>');
                      > write('</table>');
                      > write('</div>');
                      > write('</body>');
                      > write('</html>');
                      > close();
                      > setPage(pageCod e);
                      > }
                      > }
                      >
                      > ...
                      >
                      > function setPage(url) {
                      > parent.mainfram e.location.href = url.toString();
                      > }[/color]

                      I could be wrong, but I think everyone missed the obvious here. The function
                      setHome() does document.write( ). The moment the code issues document.open() ;
                      document.write( ...); the content of the current document (including the
                      function setPage()) is destroyed.

                      By the time Firefox gets to the call "setPage(pageCo de);", that function no
                      longer exists.

                      It's generally not a good idea to document.write( ) into the current document
                      after it has been closed by "</body></html>".

                      --
                      Grant Wagner <gwagner@agrico reunited.com>
                      comp.lang.javas cript FAQ - http://jibbering.com/faq

                      Comment

                      • Lasse Reichstein Nielsen

                        #12
                        Re: setPage is not defined error in Mozilla/Firefox

                        Grant Wagner <gwagner@agrico reunited.com> writes:
                        [color=blue]
                        > I could be wrong, but I think everyone missed the obvious here. The function
                        > setHome() does document.write( ). The moment the code issues document.open() ;
                        > document.write( ...); the content of the current document (including the
                        > function setPage()) is destroyed.[/color]

                        I think you are wrong (but I could be too :). As long as the Javascript
                        is executing, its execution context remains in existence. That means that
                        even after writing the document, even if the new document was put into
                        a fresh window object, the original code can still see its own variables.

                        A simple test would be:
                        ---
                        <script type="text/javascript">
                        function foo() {alert("I exist");}
                        document.open() ;
                        document.write( "<html><head><t itle>test</title></head><body>hell o");
                        document.write( "</body></html>");
                        document.close( );
                        foo();
                        setTimeout(foo, 2000);
                        </script>
                        ---
                        It works in Opera(7.6p1), Mozilla FireFox and IE 6.

                        /L
                        --
                        Lasse Reichstein Nielsen - lrn@hotpop.com
                        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                        'Faith without judgement merely degrades the spirit divine.'

                        Comment

                        • Grant Wagner

                          #13
                          Re: setPage is not defined error in Mozilla/Firefox

                          Lasse Reichstein Nielsen wrote:
                          [color=blue]
                          > Grant Wagner <gwagner@agrico reunited.com> writes:
                          >[color=green]
                          > > I could be wrong, but I think everyone missed the obvious here. The function
                          > > setHome() does document.write( ). The moment the code issues document.open() ;
                          > > document.write( ...); the content of the current document (including the
                          > > function setPage()) is destroyed.[/color]
                          >
                          > I think you are wrong (but I could be too :). As long as the Javascript
                          > is executing, its execution context remains in existence. That means that
                          > even after writing the document, even if the new document was put into
                          > a fresh window object, the original code can still see its own variables.
                          >
                          > A simple test would be:
                          > ---
                          > <script type="text/javascript">
                          > function foo() {alert("I exist");}
                          > document.open() ;
                          > document.write( "<html><head><t itle>test</title></head><body>hell o");
                          > document.write( "</body></html>");
                          > document.close( );
                          > foo();
                          > setTimeout(foo, 2000);
                          > </script>
                          > ---
                          > It works in Opera(7.6p1), Mozilla FireFox and IE 6.[/color]

                          Perhaps you are correct. I was under the impression the OP was calling setHome()
                          after the page had been closed:

                          <body>
                          <a href="#" onclick="setHom e(someUrl);retu rn false;">Reload menu</a>
                          <!-- ... -->
                          </body>

                          Rather than being called while the page was still being processed:

                          <body>
                          <script type="text/javascript">
                          setHome(someUrl );
                          </script>
                          <!-- ... -->
                          </body>

                          I just read and re-read the original post and I'm not sure you can make a
                          convincing argument either way. We'll have to wait for the OP to tell us how he
                          was invoking setHome(). However, given the behaviour he is seeing, I would not be
                          surprised to discover he is trying to use setHome() after the document has been
                          closed.

                          --
                          Grant Wagner <gwagner@agrico reunited.com>
                          comp.lang.javas cript FAQ - http://jibbering.com/faq

                          Comment

                          • Lasse Reichstein Nielsen

                            #14
                            Re: setPage is not defined error in Mozilla/Firefox

                            Grant Wagner <gwagner@agrico reunited.com> writes:
                            [color=blue]
                            > Perhaps you are correct. I was under the impression the OP was
                            > calling setHome() after the page had been closed:[/color]

                            Yes, not a very good example I made, doing a document.open() while
                            it is already open :)

                            The real page initiates the writing with a document.open() , so the
                            existing page will be cleared. My guess was that it might not be
                            completely forgotten as long as there are scripts referring to it.
                            [color=blue]
                            > <body>
                            > <a href="#" onclick="setHom e(someUrl);retu rn false;">Reload menu</a>
                            > <!-- ... -->
                            > </body>[/color]

                            Let's try the test with this change:
                            ---
                            <html><head><ti tle>Test</title></head><body>
                            <script type="text/javascript">
                            function foo() {alert("I exist");}
                            function doit() {
                            document.open() ;
                            document.write( "<html><head><t itle>test</title></head><body>hell o");
                            document.write( "</body></html>");
                            document.close( );
                            setTimeout(func tion(){alert(ty peof foo);foo();}, 2000);
                            alert(typeof foo);
                            foo();
                            }
                            </script>
                            <p><input type="button" onclick="doit() " value="do it!"></p>
                            </body></html>
                            ---

                            Now:
                            - Opera makes all four alerts and says the type is "function" both times.
                            - IE only makes the first two alerts, saying "function". The timeout is
                            completely lost.
                            - Mozilla FF makes two alerts saying "undefined" , so "foo" is gone.

                            So, you do seem to be right for Mozilla.

                            /L
                            --
                            Lasse Reichstein Nielsen - lrn@hotpop.com
                            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                            'Faith without judgement merely degrades the spirit divine.'

                            Comment

                            • Johan

                              #15
                              Re: setPage is not defined error in Mozilla/Firefox

                              Lasse Reichstein Nielsen wrote:[color=blue]
                              > Johan <johankeesAT@il seDOT.nl> writes:
                              >
                              >[color=green]
                              >>Thanks for your reply. As far as I can see I'm doing what you're
                              >>suggesting. I don't see where the newlines are, because every write
                              >>starts on a newline and spans just one line.[/color]
                              >
                              >
                              > I cut this from another reply:
                              > ---
                              > writeln('<tr><t d><a href="#"
                              > onclick="setSof tware(\'./software.html\' )">Software< \/a><\/td><\/tr>');
                              > ---
                              > The way "onclick" is indented suggests that it is on another line,
                              > and it's not just your news-client that has broken the line.
                              >
                              > I can't be sure ofcourse. Some news-clients always breaks long lines
                              > (both at the posting and the reading end), and they aren't consistent
                              > about how they do it, which is why program code posted here should not
                              > be too long (72 characters works well).
                              >
                              > What you posted does have incorrect newlines inside the string
                              > literal. That bug, if it is in the original, would give the symptoms
                              > you are experiencing.
                              >
                              > /L[/color]
                              Thanks again for replying. As far as I can see this is not the problem.
                              Every writeln is on one line. You're right about the news-client, it is
                              cutting up the lines. I don't see any other newlines (besides the
                              writeln) in my code (or am I wrong).

                              Johan

                              Comment

                              Working...