devxlogo

Determining if a ToElement Exists

Determining if a ToElement Exists

Question:
In a VBScript function I assign the window.event.ToElement to an object for some functionality. The problem is that if the mouse is moved outside of the browser window, the toElement causes an “object not found” to occur. How can I check if the toElement exists before using it, or if the mouse has moved outside of the window?

Answer:
Visual Basic is a powerful language and has a robust set of error handling routines. VBScript is a less powerful language, and its error handling capabilities are, to put up bluntly, weak. The only way to handle errors here is to use the statement:

On Error Resume Next

The Resume Next part says, “If an error occurs in the current line, then skip the next line and execute the line after that.” While you can do a certain basic amount of work with this, it makes for ugly awkward code. For example, to detect the target of a mouse movement, you could use the following bit of code (in this case, it modifies the status bar text, but it could be changed easily enough):

	On Error Test
This is a test.
This is another test.

So long as the mouse is pointing to an identifiable element, the statement “set targetObj=window.event.toElement” will assign a reference for the target element to the targetObj variable (note the Set keyword–this reference is an object). The next statement then assigns the contents of the object’s ALT attribute to the targetText variable.

Notice that before the on error statement, this same variable was set to “Unknown Element”, making it look like the code is setting the status redundantly. So long as the cursor stays over an element, this is true–the “Unknown Element” text gets replaced. However, if the cursor rolls over a non-element (such as moving out of the window) then the second assignment is skipped, and TargetText remains set to “Unknown Element”.

Note that in JavaScript, this whole issue is moot. While JavaScript doesn’t have the error handling capability that VBScript has, in most cases, this issue isn’t a problem. JavaScript makes use of an unusual property of objects in the language. If a variable has yet to be defined, then the expression “”+myObj is the same as “undefined”. You can use this to good effect in a wide number of circumstances. For example, the same code from above written in JavaScript would look like this:

	On Error Test
This is a test.
This is another test.

This script ends up taking fewer lines to accomplish the same task, and it’s considerably easier to follow. This is one of the reasons why JavaScript is better used on the client (server side ASP, which has a considerably more robust error handling mechanism, is a whole different story however).

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist