{"id":68823,"date":"2005-10-04T19:23:00","date_gmt":"2005-10-04T19:23:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/10\/04\/how-can-i-get-a-script-to-close-an-html-page\/"},"modified":"2005-10-04T19:23:00","modified_gmt":"2005-10-04T19:23:00","slug":"how-can-i-get-a-script-to-close-an-html-page","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-a-script-to-close-an-html-page\/","title":{"rendered":"How Can I Get a Script to Close an HTML Page?"},"content":{"rendered":"<p><P>&nbsp;<\/P><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! In one of my scripts I display an HTML page with information that I want my users to read. When they\u2019re 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: \u201cThe Web page you are viewing is trying to close the window. Do you want to close the window?\u201d The user then has to click <B>Yes<\/B> to get rid of the Web page. How can I fix that?<BR><BR>&#8212; RR<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, RR. By the way, congratulations: this is the longest question that we\u2019ve ever included in a <I>Hey, Scripting Guy!<\/I> column. Is there a prize for that? No, but it\u2019s certainly something you can tell your grandkids someday.<\/P>\n<P>Well, we didn\u2019t say that it would <I>impress<\/I> those grandkids. But if you\u2019re anything like the Scripting Guys (and let\u2019s hope that you aren\u2019t), then you latch on to <I>any<\/I> achievement, no matter how trivial. After all, if it wasn\u2019t for trivial achievements that don\u2019t impress anyone we\u2019d never be able to write our year-end reviews!<\/P>\n<P>At any rate, we\u2019re assuming that you have an HTML file that looks something like this:<\/P><PRE class=\"codeSample\">&lt;BODY onkeypress=&#8217;self.close()&#8217;&gt;\nPress any key to close this window.&lt;BR&gt;\n&lt;\/BODY&gt;\n<\/PRE>\n<P>Admittedly, yours is probably a <I>little<\/I> fancier, but the idea is the same: you\u2019ve got some sort of Web page with an <B>onkeypress<\/B> event attached to the &lt;BODY&gt; tag. If this page is the active Window and someone presses any key on the keyboard, then this little script will run:<\/P><PRE class=\"codeSample\">self.close()\n<\/PRE>\n<P>And that little script <I>should<\/I> close the page. As you noted, however, what happens instead is that the user sees a message box asking if they really <I>do<\/I> want to close the page. That\u2019s a security measure built into Internet Explorer: it\u2019s designed to prevent someone else from writing a script that shuts down your Web page. In this case, however, it\u2019s preventing anyone, including yourself, from using a script to close the page. (You can, of course, click the <B>Close<\/B> button in order to dismiss the page. But obviously that isn\u2019t what you want to do.)<\/P>\n<P>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 <B>.htm<\/B> to <B>.hta<\/B>. This changes your plain old HTML page into a &#8211; tah-dah! &#8211; <B>HTML Application<\/B> (HTA). HTAs run in a different process and follow a different security model than HTML pages do; because of that, the <B>self.close()<\/B> method will be able to close an HTA without generating a message box.<\/P>\n<P>That\u2019s a pretty easy fix. Unfortunately, though, it\u2019s not a <I>guaranteed<\/I> fix; that\u2019s because there are HTML pages that can\u2019t be converted to HTAs (again, due to the fact that they follow different security models). But that\u2019s OK: like we said, we know another way to work around this problem. If you don\u2019t want to or can\u2019t turn your HTML page into an HTA then all you have to do is add a <B>Window_OnLoad<\/B> subroutine, making your Web page look like this:<\/P><PRE class=\"codeSample\">&lt;SCRIPT LANGUAGE=&#8221;VBScript&#8221;&gt;\n    Sub Window_Onload\n      window.opener = &#8220;x&#8221;\n    End Sub\n&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;BODY onkeypress=&#8217;self.close()&#8217;&gt;\nPress any key to close this window.&lt;BR&gt;\n&lt;\/BODY&gt;\n<\/PRE>\n<P>Yes, it <I>is<\/I> pretty simple; in fact, the subroutine itself consists of just one line of code:<\/P><PRE class=\"codeSample\">window.opener = &#8220;x&#8221;\n<\/PRE>\n<P>The <B>Opener<\/B> property is a reference to the window that opened <I>your<\/I> window. When you pop up your Web page, the Opener property is Null. That\u2019s to be expected: after all, your Web page wasn\u2019t opened from another window. However, in Internet Explorer a script is not allowed to close a page that doesn\u2019t have an Opener. Fortunately, all you have to do is set the value of the Opener property to <I>something<\/I> (even something meaningless, like <I>x<\/I>) and your script will then work just fine.<\/P>\n<P>We should point out that our answer is based on your particular scenario: you\u2019re 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\u2019s the best we can say: it <I>appears<\/I> to work in that situation as well. But that\u2019s not so bad: after all, look how far the Scripting Guys have gotten by merely <I>appearing<\/I> to work!<\/P><BR>\n<DIV>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"\"><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/oct05\/hey1004.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/oct05\/hey1004.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Hey, Scripting Guy! In one of my scripts I display an HTML page with information that I want my users to read. When they\u2019re 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,5,30],"class_list":["post-68823","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>&nbsp; Hey, Scripting Guy! In one of my scripts I display an HTML page with information that I want my users to read. When they\u2019re 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=68823"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68823\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=68823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}