{"id":66633,"date":"2006-08-22T15:31:00","date_gmt":"2006-08-22T15:31:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/08\/22\/how-can-i-dynamically-change-the-background-color-of-an-hta\/"},"modified":"2006-08-22T15:31:00","modified_gmt":"2006-08-22T15:31:00","slug":"how-can-i-dynamically-change-the-background-color-of-an-hta","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-dynamically-change-the-background-color-of-an-hta\/","title":{"rendered":"How Can I Dynamically Change the Background Color of an HTA?"},"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 dynamically change the background color of an HTA?<BR><BR>&#8212; CN<\/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, CN. You know, last summer (Fourth of July, 2005) an errant firework shot by a neighbor accidentally caught one of the Scripting Guys\u2019 garage on fire. (Because it was shot by a neighbor we assume it was an accident. Had it been shot by one of the other Scripting Guys, well \u2026.) Although no one was hurt, the garage was totaled. Forced to rebuild said garage from scratch, the Scripting Family decided to turn it into a family room. (Admittedly, they don\u2019t <I>like<\/I> having to park the Rolls Royce outside instead of in the garage. But hey, if the Rolls gets wet they\u2019ll just buy a new one.)<\/P>\n<P>We won\u2019t bother recapping the saga of the Contractor Who Couldn\u2019t Be Bothered to Contract; suffice to say that the job that would be done \u201cin three weeks, guaranteed\u201d took six months. After six months of lethargy, punctuated by an occasional hour or two of work, the very last thing that needed to be done was to paint the interior. \u201cWhat color would you like us to paint it?\u201d asked the contractor. \u201cAnd remember, we have to paint everything the same; the insurance company will only pay for one color.\u201d<\/P>\n<P>\u201cIn that case we\u2019ll take white,\u201d replied the Scripting Guy.<\/P>\n<P>\u201cWhat shade of white?\u201d asked the contractor. <\/P>\n<P>\u201cUm, I don\u2019t know,\u201d said the Scripting Guy. \u201cJust plain old white. Any shade of white will be fine.\u201d<\/P>\n<P>A couple weeks later the Scripting Guy came home from work to discover that \u2013 miracle of miracles \u2013 the painter (Big Larry) had actually come out and painted the family room. That was the good news. The bad news: he painted the entire room \u2013 ceiling, walls, door frames, you name it \u2013 yellow.<\/P>\n<P>Not being much of an artist, the Scripting Guy had had no idea that yellow was a shade of white. Which, apparently, it is.<\/P>\n<P>The moral of the story: a color-changing script would have come in pretty handy right about then. Unfortunately, the Scripting Guy didn\u2019t have a color-changing script that would work on a family room; all he had was a color-changing script that would work on an HTML Application (HTA):<\/P><PRE class=\"codeSample\">&lt;SCRIPT LANGUAGE=&#8221;VBScript&#8221;&gt;\n    Sub TestSub\n        document.body.bgColor=&#8221;red&#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;Run Script&#8221; name=&#8221;run_button&#8221; onClick=&#8221;TestSub&#8221;&gt;\n&lt;\/body&gt;\n<\/PRE>\n<P>As you can see, there really isn\u2019t much to this HTA. The user interface, such as it is, consists solely of a button labeled <B>Run Script<\/B>; when you click that button, it triggers a subroutine named TestSub:<\/P><PRE class=\"codeSample\">&lt;input id=runbutton type=&#8221;button&#8221; value=&#8221;Run Script&#8221; name=&#8221;run_button&#8221; onClick=&#8221;TestSub&#8221;&gt;\n<\/PRE>\n<P>And what does this subroutine do? Why, it changes the background color of the HTA to red:<\/P><PRE class=\"codeSample\">Sub TestSub\n    document.body.bgColor=&#8221;red&#8221; \nEnd Sub\n<\/PRE>\n<P>Pretty simple, huh? All we\u2019re doing is grabbing the <B>document.body<\/B> object and then setting the value of the <B>bgColor<\/B> property to red. Would you rather set the background color to aquamarine? No problem:<\/P><PRE class=\"codeSample\">document.body.bgColor=&#8221;aquamarine&#8221;\n<\/PRE>\n<P>How about lavenderblush (you know, you can never go wrong with lavenderblush):<\/P><PRE class=\"codeSample\">document.body.bgColor=&#8221;lavenderblush&#8221;\n<\/PRE>\n<P>You can even \u2013 alas \u2013 set the background color to yellow:<\/P><PRE class=\"codeSample\">document.body.bgColor=&#8221;yellow&#8221;\n<\/PRE>\n<TABLE id=\"E3D\" 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>. Looking for a list of all the color values you can pass, by name, to the bgColor property? Look no further than the <A href=\"http:\/\/msdn.microsoft.com\/workshop\/author\/dhtml\/reference\/colors\/colors.asp\" target=\"_blank\"><B>HTML and DHTML Reference<\/B><\/A> on MSDN.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Incidentally, CN, you noted in your email that you tried changing the background color by modifying the InnerHTML property of the HTA body; however, you weren\u2019t able to get that to work. Why not? To answer that question, let\u2019s take a look at a simple little &lt;body&gt; tag, one that sets the background color to red:<\/P><PRE class=\"codeSample\">&lt;body bgColor=&#8221;red&#8221;&gt;\n    &lt;b&gt;This is the body of the document.&lt;b&gt;\n&lt;\/body&gt;\n<\/PRE>\n<P>As you can see, you specify the background color as part of the &lt;body&gt; tag itself. Is that important? You bet it is: that\u2019s because the InnerHTML property only applies (in this case) to the stuff that comes <I>between<\/I> the beginning and ending &lt;body&gt; tags. Our beginning tag is this:<\/P><PRE class=\"codeSample\">&lt;body bgColor=&#8221;red&#8221;&gt;\n<\/PRE>\n<P>Our ending tag is this:<\/P><PRE class=\"codeSample\">&lt;\/body&gt;\n<\/PRE>\n<P>That means that the InnerHTML property will affect only this portion of the HTA:<\/P><PRE class=\"codeSample\">&lt;b&gt;This is the body of the document.&lt;b&gt;\n<\/PRE>\n<P>Using the InnerHTML property we can add buttons and check boxes and images and do all sorts of fun things. However, we can\u2019t change the background color, assign a background picture, or do anything else to the &lt;body&gt; tag itself. We can do that only by directly accessing the properties of the document.body object. That\u2019s why modifying the InnerHTML property didn\u2019t do anything to change the background color.<\/P>\n<TABLE id=\"E1E\" 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>. As a matter of fact, there <I>is<\/I> a property named OuterHTML, a property that can be used to completely replace an HTML object, including the starting and ending tags. However, the document.body object doesn\u2019t support the OuterHTML property.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As long as we\u2019re on the subject, here\u2019s a nifty little HTA. We won\u2019t explain the code today, but when you click the button in <I>this<\/I> HTA a color picker dialog box appears on screen. Select a color, click <B>OK<\/B>, and the background color of the HTA will change to the color you selected in the dialog box (really; give it a try and see for yourself). Here\u2019s the HTA code:<\/P><PRE class=\"codeSample\">&lt;SCRIPT LANGUAGE=&#8221;VBScript&#8221;&gt;\n    Sub TestSub\n        intColor=dlgHelper.ChooseColorDlg    \n        If intColor &lt;&gt; 0 then\n            document.body.bgColor=intColor\n        End If\n    End Sub\n&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body&gt;\n    &lt;OBJECT id=dlgHelper CLASSID=&#8221;clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b&#8221; width=&#8221;0px&#8221; height=&#8221;0px&#8221;&gt;&lt;\/OBJECT&gt; \n    &lt;input id=runbutton type=&#8221;button&#8221; value=&#8221;Run Script&#8221; name=&#8221;run_button&#8221; onClick=&#8221;TestSub&#8221;&gt;\n&lt;\/body&gt;\n<\/PRE>\n<P>And here\u2019s where you can find more information on the <A href=\"http:\/\/msdn.microsoft.com\/workshop\/author\/dhtml\/reference\/objects\/dialoghelper.asp\" target=\"_blank\"><B>Dialog Helper object<\/B><\/A> that makes this all possible.<\/P>\n<P>As for the Scripting Guy\u2019s family room, he didn\u2019t bother to tell the contractor that yellow wasn\u2019t actually a shade of white; having taken six months to finally get the job done he had no desire to wait another six months for Big Larry to get around to repainting the room. Instead, the Scripting Guy left part of the room yellow, painted a couple walls a reddish-color, and painted the trim and a few odds-and-ends white (the <I>real<\/I> white). Surprisingly enough, it turned out really nice, which means that this story has a happy ending.<\/P>\n<P>Or at least as happy an ending as you can get when you\u2019re dealing with contractors and insurance jobs.<\/P>\n<TABLE id=\"E6F\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>More Information.<\/B> For more on creating HTML Applications, see the <A href=\"http:\/\/null\/technet\/scriptcenter\/hubs\/htas.mspx\"><B>HTA Developers Center<\/B><\/A>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I dynamically change the background color of an HTA?&#8212; CN Hey, CN. You know, last summer (Fourth of July, 2005) an errant firework shot by a neighbor accidentally caught one of the Scripting Guys\u2019 garage on fire. (Because it was shot by a neighbor we assume it was an accident. [&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-66633","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 dynamically change the background color of an HTA?&#8212; CN Hey, CN. You know, last summer (Fourth of July, 2005) an errant firework shot by a neighbor accidentally caught one of the Scripting Guys\u2019 garage on fire. (Because it was shot by a neighbor we assume it was an accident. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66633","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=66633"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66633\/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=66633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}