Function not defined error in Mozilla

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KreskinsFolly
    New Member
    • Apr 2010
    • 1

    Function not defined error in Mozilla

    I have just started building this small web page and I'm getting a Javascript error I can't account for.
    This is the HTML document:


    Code:
    <html>
    <head>
    <script type="text/javascript" src="scripts.js"></script>
    </head>
    
    <body onLoad="pageLoad()">
    </body>
    
    </html>

    nothing too mysterious there. The body tag calls the external function 'pageLoad()'
    And here's the Javascript document:


    Code:
    function pageLoad()
    {
    loadCHSheet(); //load image
    }
    
    function loadCHSheet()
    {
    document.write ('<img id="sheet" src="front.bmp" onClick="goPage()">');
    }
    
    function goPage()
    {
    alert('test');
    }

    as you can see, the body tag uses the 'onLoad' event to call the 'loadPage()' function, which then calls the 'loadCHSheet()' function, which then uses 'document.write ' to create an image tag with an 'onClick' event.

    The image loads perfectly, but when clicked nothing happens and the javascript error box shows the error "goPage is not defined".

    I changed the 'onClick' event to call an alert instead and it worked perfectly.

    I'm completely stumped, please help. Thanx.
    Last edited by Dormilich; Apr 15 '10, 06:09 AM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    that’s because you don’t load the JavaScript.

    confused?

    document.write( ) rewrites the whole document (erases all previous content), if that document finished loading before calling document.write( ).

    do yourself a favour and learn, how you can alter the HTML document using DOM methods. check out the offsite links for a suitable tutorial.

    Comment

    Working...