Use the VBS script to close the HTML page code _vbs
Source: Internet
Author: User
Ask:
Hello, Scripting Guy! In one of my scripts, I display an HTML page that contains the information I want the user to read. When they finish reading the page, they should be able to press a key to make the page disappear. However, instead of disappearing, a message box appears saying, "The page you are viewing is trying to close the window." Do you want to close the window? "Then the user must click" Yes "to turn off the page. How do I solve the problem?
--RR
For:
Hello, RR. By the way congratulations: this is our "Hello, Scripting Guy!" "The longest question in the column. Don't know if there's a reward? No, but there is one thing to be sure that you can tell your grandson some day.
We did not say that it would impress your grandson. But if you're someone like the Scripting Guys (hopefully you're not), you'll never let go of anything, even trivial. After all, if there is no such trivial result that doesn't impress anyone, we can't write the year-end summary!
Anyway, suppose you have an HTML file that looks like this:
<body onkeypress= ' self.close () ' >
Press any key to close this window.<br>
</BODY>
Of course, your page may be more imaginative, but the idea is the same: you've got some kind of web page with a onkeypress event attached to the <BODY> tag. If this page is the active window and someone presses any key on the keyboard, the following small script runs:
Self.close ()
This small script will close the page. However, as you noted, users will see a message box appear asking if they are sure they want to close the page. This is a security method built into Internet Explorer: It's primarily to prevent others from writing scripts to close your Web page. In this case, however, it prevents anyone (including yourself) from using the script to close the page. (You can, of course, click the Close button to close the page.) But obviously, this is not what you want to do. )
So, how do you solve this problem? In fact, we know two ways to solve this problem. The first method is that you can simply change the file name extension from. htm to an. hta. As a result, the original, prosaic HTML page is transformed into a –html application (HTA). The HTA uses a completely different run process and security model than the HTML page. Therefore, the Self.close () method will be able to shut down the HTA without generating a message box.
This is a very simple workaround. Unfortunately, however, this is not a "foolproof" solution; because they use different security models, some HTML pages cannot be converted to HTA. But it doesn't matter: as we said, there is another way to solve this problem. If you don't want to or can't convert an HTML page to an HTA, all you have to do is add a Window_onload subroutine, so that your page looks 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's really very simple; in fact, the subroutine itself contains only one line of code:
Window.opener = "X"
The opener property is a reference to a window that has your window open. When you eject your Web page, the opener property is Null. This is what is expected: After all, your Web page is not opened from another window. However, in Internet Explorer, scripts are not allowed to close pages that do not have opener. Fortunately, all you have to do is set the value of the opener property to a value (even if the values are meaningless, like X), and your script will work correctly.
It should be noted that our answer is based on your special case: You are opening an HTML file from the desktop. It seems that this method can also be used for HTML pages provided by the WEB server. However, we only provide a basic test, so we can only say: It also seems to apply to the situation. But that's fine: after all, it's a great achievement for the Scripting Guys, though it just seems to work!
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.