{"id":68563,"date":"2005-11-09T14:02:00","date_gmt":"2005-11-09T14:02:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/11\/09\/how-can-i-clear-an-hta-window\/"},"modified":"2005-11-09T14:02:00","modified_gmt":"2005-11-09T14:02:00","slug":"how-can-i-clear-an-hta-window","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-clear-an-hta-window\/","title":{"rendered":"How Can I Clear an HTA Window?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I clear an HTA window?<BR><BR>&#8212; DF<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, DF. Well, to tell you the truth, if you\u2019d ever seen at least one of the Scripting Houses you wouldn\u2019t be asking <I>us<\/I> if we knew how to clear a window; it\u2019s pretty obviously some of us don\u2019t. (In our defense, though, if we keep the window clean then birds have a terrible habit of flying headlong into it. Which, of course, is also why we don\u2019t vacuum the rug very often: we don\u2019t want birds flying headlong into the rug, either.)<\/P>\n<P>We\u2019re actually going to answer your question by showing you two scripts: one script that clears the entire window and another that clears only <I>part<\/I> of the window. Why are we going to do that? Well, we can\u2019t tell you that just yet; that would spoil the surprise, wouldn\u2019t it?<\/P>\n<P>Let\u2019s start off with a simple HTA, one that features a button that, when clicked, clears the entire HTA window:<\/P><PRE class=\"codeSample\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;HTA Test&lt;\/title&gt;\n&lt;HTA:APPLICATION \n     ID=&#8221;objTest&#8221; \n     APPLICATIONNAME=&#8221;Clear Window&#8221;\n     SCROLL=&#8221;yes&#8221;\n     SINGLEINSTANCE=&#8221;yes&#8221;\n&gt;\n&lt;\/head&gt;<\/p>\n<p>&lt;SCRIPT LANGUAGE=&#8221;VBScript&#8221;&gt;\n    Sub ClearWindow\n        document.body.InnerHTML = &#8220;&#8221;\n    End Sub  \n&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body&gt;\n    &lt;input id=runbutton type=&#8221;button&#8221; value=&#8221;Clear&#8221; onClick=&#8221;ClearWindow&#8221;&gt;\n&lt;\/body&gt;\n<\/PRE>\n<P>You\u2019re right: there <I>isn\u2019t<\/I> much to this. We start off by specifying a title for our window, using this line of code:<\/P><PRE class=\"codeSample\">&lt;title&gt;HTA Test&lt;\/title&gt;\n<\/PRE>\n<P>We then have some optional lines of code that are used to configure various properties of the HTA itself. We won\u2019t discuss those in any detail today; for more information you might check out the <A href=\"http:\/\/null\/technet\/scriptcenter\/hubs\/htas.mspx\"><B>HTA Developers Center<\/B><\/A> or the Scripting Week 3 webcast <A href=\"http:\/\/msevents.microsoft.com\/CUI\/EventDetail.aspx?EventID=1032282307&amp;Culture=en-US\" target=\"_blank\"><B>Going Beyond the Command Line with HTML Applications<\/B><\/A>.<\/P>\n<P>As you can see, the body of the HTA consists of a single element: a button labeled <B>Clear<\/B>:<\/P><PRE class=\"codeSample\">&lt;input id=runbutton  type=&#8221;button&#8221; value=&#8221;Clear&#8221; onClick=&#8221;ClearWindow&#8221;&gt;\n<\/PRE>\n<P>Click that button, and a subroutine named ClearWindow will fire up.<\/P>\n<P>And what happens inside that subroutine? Not much; in fact, all we do is set the InnerHTML property of the <B>document.body<\/B> object to nothing (\u201c\u201d):<\/P><PRE class=\"codeSample\">document.body.InnerHTML = &#8220;&#8221;\n<\/PRE>\n<P>And that, in turn, clears the HTA window. You want to clear an HTA window? Then just set the document.body.InnerHTML property to an empty string.<\/P>\n<P>But wait, don\u2019t go yet: we need to emphasize that this really <I>clears<\/I> the HTA window. For example, here\u2019s what our HTA looks like before we click the button:<\/P><IMG border=\"0\" alt=\"HTA\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/clear.jpg\" width=\"335\" height=\"191\"> \n<P><BR>And here\u2019s what it looks like <I>after<\/I> we click the button:<\/P><IMG border=\"0\" alt=\"HTA\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/clear2.jpg\" width=\"335\" height=\"191\"> \n<P><BR>The window\u2019s been cleared all right: in fact, <I>everything<\/I> has been erased, including our <B>Clear<\/B> button. There will likely be times when you want everything &#8211; including your interface elements &#8211; to be erased. However, there will also be times when you just want certain items &#8211; say, data retrieved from a previous script &#8211; to be erased while other items (such as buttons) are left alone. So let\u2019s show you a variation of this script, one that erases only a portion of the HTA window.<\/P>\n<TABLE id=\"E6E\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Of course, you can always restore the user interface by pressing <B>F5<\/B>, thus refreshing the HTA window.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Here\u2019s the revised code:<\/P><PRE class=\"codeSample\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;HTA Test&lt;\/title&gt;\n&lt;HTA:APPLICATION \n     ID=&#8221;objTest&#8221; \n     APPLICATIONNAME=&#8221;Clear Window&#8221;\n     SCROLL=&#8221;yes&#8221;\n     SINGLEINSTANCE=&#8221;yes&#8221;\n&gt;\n&lt;\/head&gt;<\/p>\n<p>&lt;SCRIPT LANGUAGE=&#8221;VBScript&#8221;&gt;\n    Sub ClearWindow\n        DataArea.InnerHTML = &#8220;&#8221;\n    End Sub  \n&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body&gt;\n    &lt;input id=runbutton  type=&#8221;button&#8221; value=&#8221;Clear&#8221; onClick=&#8221;ClearWindow&#8221;&gt;\n    &lt;p&gt;\n    &lt;span id=DataArea&gt;This is the data area.&lt;\/span&gt;\n&lt;\/body&gt;\n<\/PRE>\n<P>For the most part we\u2019ve done two things here. First, we added a <B>&lt;span&gt;<\/B> tag and given this span the id DataArea. A span, as you probably know, is a named-area of the HTA that we can dynamically modify; for example, using a script we can add text (and HTML formatting) to the span.<\/P>\n<P>Or, we can <I>clear<\/I> the span of all text and HTML formatting. That\u2019s the second change we made to our HTA: we\u2019ve rewritten the ClearWindow subroutine so that instead of clearing the entire window it clears only the DataArea span:<\/P><PRE class=\"codeSample\">Sub ClearWindow\n    DataArea.InnerHTML = &#8220;&#8221;\nEnd Sub\n<\/PRE>\n<P>Why did we do that? Well, here\u2019s what our revised HTA looks like when it first loads:<\/P><IMG border=\"0\" alt=\"HTA\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/clear3.jpg\" width=\"336\" height=\"196\"> \n<P><BR>As you can see, our span (located just below the button) features the words <B>This is the data area<\/B>.<\/P>\n<P>Now, here\u2019s what the HTA looks like after we click the button:<\/P><IMG border=\"0\" alt=\"HTA\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/clear.jpg\" width=\"335\" height=\"191\"> \n<P><BR>Ah, now you get it: we cleared the DataArea, but left the window unchanged. That means our UI elements (in this simple example, that\u2019s just the <B>Clear<\/B> button) are still there and still available for use.<\/P>\n<P>And, yes, that also means that no birds will fly headlong into this HTA, either. One less thing to worry about!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I clear an HTA window?&#8212; DF Hey, DF. Well, to tell you the truth, if you\u2019d ever seen at least one of the Scripting Houses you wouldn\u2019t be asking us if we knew how to clear a window; it\u2019s pretty obviously some of us don\u2019t. (In our defense, though, if [&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-68563","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>Hey, Scripting Guy! How can I clear an HTA window?&#8212; DF Hey, DF. Well, to tell you the truth, if you\u2019d ever seen at least one of the Scripting Houses you wouldn\u2019t be asking us if we knew how to clear a window; it\u2019s pretty obviously some of us don\u2019t. (In our defense, though, if [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68563","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=68563"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68563\/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=68563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}