VBScript

Combining VBScript and JavaScript

One of the things that many scripters want to be able to do is combine VBScript and another scripting language, usually JavaScript. The worry seems to be that the browser will only be able to initiate one scripting engine or that having two running side-by-side will slow the users system down to a crawl.

None of this is true - the browser is perfectly capable of initializing two or more script engines for a single web page. The performance hit on the system is negligible at most. So you can stop worrying and get on and combine scripts!

Here's an example of how it can be done, we start with a basic web page:

<html>
<head>
<title>VBScript/JavaScript combo example</title>
</head>
<body>
</body>
</html>

Next, we add a simple VBScript code block, remembering the comment tags:

<html>
<head>
<title>VBScript/JavaScript combo example</title>
  <script language="vbscript">
  <!--
  msgbox "VBScript working!"
  -->
  </script>
</head>
<body>
</body>
</html>
Next, a simple JavaScript script (remembering the comment tags!):
<html>
<head>
<title>VBScript/JavaScript combo example</title>
  <script language="vbscript">
  <!--
  msgbox "VBScript working!"
  -->
  </script>
  <script language="JavaScript">
  <!--
  alert("JavaScript working!")
  //-->
  </script>
</head>
<body>
</body>
</html>

Now you can save the web page and load it in Internet Explorer. Two things will become apparent:

  • Both scripts run!
  • The VBScript script is run first.

Why is the VBScript script run first? Simple, because it appears first in the code. if you wanted the JavaScript code to run before the VBScript code, swap their order around.

There are huge advantages to combining scripts:

  • You can write custom code easily for Internet Explorer set apart from JavaScript for other browsers.
  • You can write the code code for all browsers in JavaScript and add Internet Explorer specific code in as VBScript so it's easy to distinguish.

Another thing not to worry about is combining server-side VBScript for ASP and client-side JavaScript - that's not a problem either!

Adrian Kingsley-Hughes
Last updated: July 14th 2004
Print This Page   |   Email me when this page changes    |  Search This Site



Crucial.com System Scanner does the work for you!



/a_global/close_links.inc"); ?>

Contact Us