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

ID: Q152981


The information in this article applies to:


SYMPTOMS

Microsoft Internet Explorer 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.

Additional query words: kbdse kndsi vbscript vbs


Keywords          : kbIE500 vbObjMdlIE vbsEnvtIE 
Version           : 1.00 1.10 2.00
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: July 2, 1999