Wierd Javascript Enigma

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tamlin
    New Member
    • May 2006
    • 2

    Wierd Javascript Enigma

    This is the strangest thing.
    Here is my test code

    [HTML]<HTML>
    <HEAD>
    <script type="applicati on/x-javascript">
    <!--
    function myc()
    { document.write( "hello 3<BR>");
    }

    function myb()
    { document.write( "hello 2<BR>");
    }

    function mya()
    { document.write( "hello 1<BR>");
    }

    function main()
    {
    // mya();
    // myb();
    myc();
    document.write( "hello world<BR>");
    }

    // -->
    </script>
    </HEAD>
    <BODY onload="main(); ">
    </BODY></HTML>
    [/HTML]
    This outputs a page with "hello 3" and "hello world" on it
    but if I uncomment mya() as well I only get "hello 1" and am told that myc is not defined

    Stranger still is that if I then comment out myc() I get both "hello 1" and "hello world"

    Can any one shed some light on this puzzle?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    No such problem here.

    Perhaps the problem at your end is that the type attribute should be "text/javascript".

    Comment

    • pronerd
      Recognized Expert Contributor
      • Nov 2006
      • 392

      #3
      You have two differnt issues.

      1. "applicatio n/x-javascript" is not a valid script type.

      2. You have a broken, or open, string. Strings must be closed on the same line. So this....

      Code:
      function myc()
      { document.write("hello 3<BR>
      ");
      }


      Should be

      Code:
      function myc()
      { document.write("hello 3<BR>");
      }


      If you need to drop to the next line you need to do something like this.

      Code:
      function myc()
      { document.write("hello 3<BR>" +
      " blah blah.");
      }

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by pronerd
        2. You have a broken, or open, string. Strings must be closed on the same line. So this....

        Code:
        function myc()
        { document.write("hello 3<BR>
        ");
        }


        Should be

        Code:
        function myc()
        { document.write("hello 3<BR>");
        }


        If you need to drop to the next line you need to do something like this.

        Code:
        function myc()
        { document.write("hello 3<BR>" +
        " blah blah.");
        }
        True, but if you check the actual code, it's on one line. It's probably a spacing bug when using code tags that makes it appear as though the closing quote is on a separate line. Good spot all the same :)

        Comment

        • mrhoo
          Contributor
          • Jun 2006
          • 428

          #5
          but if I uncomment mya() as well I only get "hello 1" and am told that myc is not defined

          You call main onload.
          the function definition for main is now in the same scope as
          the rest of the script, everything is defined for that window.

          Inside main, you call mya() to document.write something

          The window closes, and opens a new window with the document.write output-
          There is nothing but 'hello1'- no scripts, no onload event- check the source view.

          the window scope has changed, but the function main is still running.
          It continues and looks for myc()- which no longer exists...

          You need to either put everything in the main function, or document write the missing myc into the next window.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Right on the money.

            The strange thing though is that it does run without errors (on Firefox, at least).

            Comment

            • mrhoo
              Contributor
              • Jun 2006
              • 428

              #7
              Yeah, it is worth a think-
              what scope has an error from a function that was defined in a window that has since closed?

              Maybe the OS ends up with it, or maybe it just bubbles away out of reach.

              Comment

              Working...