October 4th, 2005

How Can I Get a Script to Close an HTML Page?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! In one of my scripts I display an HTML page with information that I want my users to read. When they’re finished reading that page, they should be able to press a key and have the Web page disappear. However, instead of the Web page disappearing they get a message box with this message: “The Web page you are viewing is trying to close the window. Do you want to close the window?” The user then has to click Yes to get rid of the Web page. How can I fix that?

— RR

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RR. By the way, congratulations: this is the longest question that we’ve ever included in a Hey, Scripting Guy! column. Is there a prize for that? No, but it’s certainly something you can tell your grandkids someday.

Well, we didn’t say that it would impress those grandkids. But if you’re anything like the Scripting Guys (and let’s hope that you aren’t), then you latch on to any achievement, no matter how trivial. After all, if it wasn’t for trivial achievements that don’t impress anyone we’d never be able to write our year-end reviews!

At any rate, we’re assuming that you have an HTML file that looks something like this:

<BODY onkeypress=’self.close()’>
Press any key to close this window.<BR>
</BODY>

Admittedly, yours is probably a little fancier, but the idea is the same: you’ve got some sort of Web page with an onkeypress event attached to the <BODY> tag. If this page is the active Window and someone presses any key on the keyboard, then this little script will run:

self.close()

And that little script should close the page. As you noted, however, what happens instead is that the user sees a message box asking if they really do want to close the page. That’s a security measure built into Internet Explorer: it’s designed to prevent someone else from writing a script that shuts down your Web page. In this case, however, it’s preventing anyone, including yourself, from using a script to close the page. (You can, of course, click the Close button in order to dismiss the page. But obviously that isn’t what you want to do.)

So how can you fix this? Well, we actually know of two ways to do this. For one, you can simply change the file extension from .htm to .hta. This changes your plain old HTML page into a – tah-dah! – HTML Application (HTA). HTAs run in a different process and follow a different security model than HTML pages do; because of that, the self.close() method will be able to close an HTA without generating a message box.

That’s a pretty easy fix. Unfortunately, though, it’s not a guaranteed fix; that’s because there are HTML pages that can’t be converted to HTAs (again, due to the fact that they follow different security models). But that’s OK: like we said, we know another way to work around this problem. If you don’t want to or can’t turn your HTML page into an HTA then all you have to do is add a Window_OnLoad subroutine, making your Web page look like this:

<SCRIPT LANGUAGE=”VBScript”>
    Sub Window_Onload
      window.opener = “x”
    End Sub
</SCRIPT>

<BODY onkeypress=’self.close()’> Press any key to close this window.<BR> </BODY>

Yes, it is pretty simple; in fact, the subroutine itself consists of just one line of code:

window.opener = “x”

The Opener property is a reference to the window that opened your window. When you pop up your Web page, the Opener property is Null. That’s to be expected: after all, your Web page wasn’t opened from another window. However, in Internet Explorer a script is not allowed to close a page that doesn’t have an Opener. Fortunately, all you have to do is set the value of the Opener property to something (even something meaningless, like x) and your script will then work just fine.

We should point out that our answer is based on your particular scenario: you’re opening up an HTML file from the desktop. It appears that this also works with an HTML page served up by a Web server. However, we only gave that a rudimentary test, so that’s the best we can say: it appears to work in that situation as well. But that’s not so bad: after all, look how far the Scripting Guys have gotten by merely appearing to work!


Author

0 comments

Discussion are closed.