PRB: Placement of <Script> Tag Results in Unexpected Behavior

Last reviewed: October 29, 1996
Article ID: Q152981
The information in this article applies to:
  • Microsoft Visual Basic, Scripting Edition, versions 1.0, 1.1, 2.0

SYMPTOMS

Microsoft Internet Explorer (IE) supports both JavaScript and VBScript functionality. For compatibility reasons, the default is JavaScript. If a block of <Form> code is placed ahead of a block of <script> code, errors may result.

RESOLUTION

To prevent these errors, always place <script> tags ahead of any objects that call into functions defined with the tags. By moving the <script> tag and associated code ahead of the form definition, the VBScript compiler is invoked and the Button element correctly references the function it calls in the onClick event.

STATUS

This behavior is by design. Internet Explorer 3.0 defaults to JavaScript compilation.

MORE INFORMATION

As an HTML page containing VBScript or JavaScript, code is loaded by IE 3.0, it is parsed and compiled. If a form element makes a call to some function that is later defined within a <Script> tag, errors can result if the form element does not use the proper calling conventions.

IE 3.0 defaults to using the JavaScript compiler. When the compiler sees the form element, it generates JavaScript code to make the function call associated to the form element. If the <Script> tag is defined after the form element and specifies Language="VBScript," this function call is syntactically incorrect and an error results.

To avoid this error, all <Script> tags should be defined before any form or other objects that make functions calls. The preferred placement of the <Script> tag is within the <Head></Head> tags. By using this placement, any function that may be called by a forms element has already been loaded and compiled.

Steps to Reproduce Behavior

The HTML code below demonstrates the problem:

   <HTML>
   <Head><title>a test</title></Head>
   <body>

   <form name="vbtest">
   <input type="button" Name="Button1" value="Press Me"
   onclick="btnPress">
   </form>

   <Script language="VBScript">
   sub btnPress
       alert "you pressed the button"
   end sub
   </script>
   </body>
   </HTML>

Because the compiler reads the form definition first, it compiles the onClick event into a JavaScript reference. When the button is clicked, the interpreter tries to find the appropriate JavaScript function to execute. However, the function is in a block of VBScript code and is not seen by the interpreter as executable JavaScript code. Therefore, an "object doesn't support the property or method" error results.


KBCategory: kbprg kbprb
KBSubcategory: vbsEnvtIE vbObjMdlIE
Additional reference words: 1.00 1.10 2.00 kbdse kndsi vbscript vbs



THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 29, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.