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).

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as