Use the vbs script to close the HTML page code

Source: Internet
Author: User

Q:
Hello, script expert! In one of my scripts, I display an HTML page that contains information that I want users to read. After reading the page, they should be able to press a key to make the page disappear. However, the webpage does not disappear, but a message box appears, prompting: "The webpage you are viewing is trying to close the window. Do you want to close this window ?" Then, you must click "yes" to close the webpage. How can this problem be solved?

-- RR

A:
Hello, RR. By the way, congratulations! This is our "Hello, script expert !" The longest problem encountered in the column. I wonder if there is any reward? No, but it is certain that one day you can tell this story to your grandson.

We didn't say it would impress your grandson. However, if you are a script expert (hopefully you are not), you will never miss any score, even if it is insignificant. After all, if we don't have any insignificant achievements that don't impress anyone, we won't be able to write a year-end summary!

In any case, assume that you have an HTML file as follows:

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

Of course, your page may be more whimsical, but the idea is the same: You have obtained a webpage with an onkeypress event attached to the <BODY> tag. If the page is an active window and someone presses any key on the keyboard, the following script is run:

Self. close ()

This script will close the page. However, as you noted, the user will see a message box and ask whether they really want to close the page. This is a built-in security measure in Internet Explorer: it is mainly used to prevent others from writing scripts to close your webpage. However, in this case, it will prevent anyone (including yourself) from closing the page using scripts. (Of course, you can click "close" to close the page. But obviously, this is not what you want to do .)

So, how to solve this problem? In fact, we know two ways to solve this problem. First, you can simply change the file extension from. htm to. hta. As a result, the original plain HTML page is converted to-HTML Application (HTA ). Compared with HTML pages, HTA uses completely different processes and security models. Therefore, the self. close () method can disable HTA without generating a message box.

This is a very simple solution. However, unfortunately, this is not a safe solution; it is precisely because they adopt different security models that 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 do not want to or cannot convert an HTML page to HTA, add a Window_OnLoad subroutine to make your webpage as follows:

<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, this is indeed very simple; in fact, this subroutine itself contains only one line of code:

Window. opener = "x"

The Opener attribute is a reference to a window that has opened your window. When you pop up your webpage, the Opener attribute is Null. This is expected: after all, Your webpage is not opened from another window. However, in Internet Explorer, scripts are not allowed to close pages without Opener. Fortunately, all you need to do is set the value of the Opener attribute to a value (even if these values are meaningless, such as x ), then your script will work properly.

It should be noted that our answer is based on your special situation: you open HTML files from the desktop. It seems that this method can also be used for HTML pages provided by the Web server. However, we only provide one basic test, so we can only say that it also seems to be applicable to this situation. But this is also good: after all, although it only seems feasible, it is a great achievement for script experts!

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.