Detecting When a Scriptlet is Running

A scriptlet is an HTML or ASP page, and can also be displayed as stand-alone document. If you do this, then you might occasionally incur a system error due to a property or a method that Internet Explorer 4.0 doesn't find. This only happens if your scriptlet attempts to access its parent environment. (more on this in Chapter 5 – Container and Ambient Properties.) To work round this you should discover a way to detect whether a given page is viewed as a scriptlet or not. If the answer is yes, then you can safely access all the attributes you need. Otherwise, you'll need to avoid doing it.

The critical point is the external object exposed by the DHTML window object. Through this, you can access some of the properties and methods of the container's environment. Simply, you ought to skip over it if the scriptlet page is viewed as a stand-alone document.

To be more precise, the error occurs only from within VBScript code. The JScript's engine traps the exception and resumes the execution without any kind of warning. This behavior, however, might also cause undesirable side-effects in some circumstances. In conclusion, it's far better to provide a Boolean variable as a safeguard and access the external object only when absolutely safe.

One-time Access To the external Object

Since the external object gets initialized if, and only if, an HTML page is running as a scriptlet, to detect this circumstance we need to access it. Using JScript saves us from a message box in case of error. We just need to check it once and then store the result in a global variable for further use.

We can choose any of the properties exposed by external and check its type. The JScript's engine returns undefined if it fails to access the object. What we can do then is just:

var InScriptlet = (typeof(window.external.version)=="string")

The variable will contain True or False, according to the type of the property external.version. Usually this property contains a string, which denotes the current version of the scriptlet engine. At the moment this string is 4.0 Win32.

The complete source code for our first scriptlet becomes:

<html id=MyPage>

<head>
<title>Our First Scriptlet</title>
</head>

<SCRIPT language=VBScript for=window event=onload>
  document.bgColor = mBackColor   
</SCRIPT>

<script language="Javascript">
public_description = new CreateFirstScriptlet;

var mBackColor = "lightcyan";
var InScriptlet = (typeof(window.external.version)=="string") 

function CreateFirstScriptlet() {
  this.get_BackColor = get_BackColor;
  this.put_BackColor = put_BackColor;
  this.TellMeSomething = HelloWorld;
  this.MyDocument = window.document;
}

function DefaultTitle( s ) {
  DefaultTitle = s;
  return mBackColor;
}
function get_BackColor() {
  return mBackColor;
}
function put_BackColor( cBackColor ) {
  mBackColor = cBackColor;
  document.bgColor = mBackColor;
  return 1;
}
function HelloWorld() {
  alert( "Hello, World!" ); 
}
</script>

<body>
Hello, World!
</body>
</html>

© 1997 by Wrox Press. All rights reserved.