RegisterLog In/Log OutView Cart
O'Reilly Ron's VB Forum Ron's VB Forum
BooksSafari BookshelfConferencesO'Reilly NetworkO'Reilly GearLearning Lab
 


Traveling to
a tech show?

Hotel Search
Hotel Discounts
Discount Hotels
Chicago Hotels
Canada Hotels
California Hotels
Hotels




Date: June 2000
From: Angelo
To: ron@oreilly.com
Subject: How can I close a window?

VBScript question: how can I close a window? I used: window.open "url", "new_window" to create it.. how do I close it?

Thanks.


Hi Angelo,

I assume that what you want to do is open a new window from an existing window that includes a client-side VBScript, as well as to close that window from the existing window whose script was used to open it. In fact, that's quite easy to do.

It's important here to understand the difference between functions and procedures, as well as to understand that most scripting takes advantage of a technology called automation, which gives you control over a particular software object through its exposed interfaces.

Let's begin with the difference between functions and procedures, which is really quite simple: functions return a value, while procedures don't. However, any function can be treated as a procedure by simply discarding its value. For instance, imagine that we have the following DivideBy2 function, which returns True if it succeeds and False otherwise:

Function DivideBy2(num)

On Error Resume Next

num = num / 2
If Err.Number <> 0 Then
   DivideBy2 = False
Else
   DivideBy2 = True
End If

End Function

We can then call it with code like the following:

Dim var1
var1 = 4

if Divideby2(var1) Then
   MsgBox var1
else
   MsgBox "An error occurred."
End If

But this simple function is almost idiot-proof: as long as we pass it a numeric value, it should execute without error. So we can safely suspend the examination of thef function's return value by changing our code to the following:

Dim var1

var1 = 4
Divideby2 var1
MsgBox var1

In other words, we've changed a function into a procedure by not saving its return value.

You may be wondering what this has to do with the problem of controlling an Internet Explorer window from the instance of Internet Explorer that opened it. The answer is that the Open method, which you're calling to open a new window, is a function, not a procedure. Click here for its actual syntax.

In other words, the Open method is returning a value, in this case a reference to the Window object that represents the new instance of the Internet Explorer window.

Once your code has a valid reference to a particular object, you have control of that object--at least to the degree that its public properties and methods allow you to control it. This technology, which is extremely powerful, is known as automation.

In any case, once you have a reference to another instance of Internet Explorer, it's easy to close it by calling its Close method. So if you named the new Internet Explorer object reference oWin, you'd close it from the original instance with a piece of code like:

oWin.Close
A complete code sample that illustrates opening a Window, retaining a reference to its Window object, and calling that Window object's Close method to destroy the new window is the following: click here for code.

Note, incidentally, that you must make the reference to the new Window object available outside of the procedure in which it's instantiated (in this case, the Window_OnClick event procedure). Otherwise, the object reference will go out of scope, and you won't have any way to control the new window.

I hope that this both helps solve your immediate programming problem, as well as introduces you to the world of programming objects with VBScript. For more information, you might want to take a look at the book that I co-authored with Paul Lomax and Matt Childs, VBScript in a Nutshell.

--Ron

Return to: Ron's Archive





O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.