{"id":68873,"date":"2005-09-27T19:37:00","date_gmt":"2005-09-27T19:37:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/09\/27\/how-can-i-periodically-refresh-a-web-page\/"},"modified":"2005-09-27T19:37:00","modified_gmt":"2005-09-27T19:37:00","slug":"how-can-i-periodically-refresh-a-web-page","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-periodically-refresh-a-web-page\/","title":{"rendered":"How Can I Periodically Refresh a Web Page?"},"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 periodically refresh a Web page?<BR><BR>&#8212; TZ<\/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, TZ. You know, the last couple of weeks have been very difficult for the Scripting Guys; after all, we were <I>positive<\/I> that one of us would be nominated to become the next Chief Justice of the United States Supreme Court. (We even bought the robes and everything, robes which &#8211; for reasons unknown &#8211; Peter won\u2019t let us return.) Instead, we\u2019re forced to sit and watch John Roberts answer questions about strict constructionism and the applicability of legal precedent. Talk about easy! We\u2019d like to see John Roberts answer a question like \u201cHow can I periodically refresh a Web page?\u201d<\/P>\n<P>Oh, right: you\u2019d probably like to see <I>us<\/I> answer a question like \u201cHow can I periodically refresh a Web page?\u201d Okey-doke:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Set objExplorer = CreateObject(&#8220;InternetExplorer.Application&#8221;)<\/p>\n<p>objExplorer.Navigate &#8220;http:\/\/www.microsoft.com\/technet\/scriptcenter&#8221;   \nobjExplorer.Visible = 1<\/p>\n<p>Wscript.Sleep 5000<\/p>\n<p>Set objDoc = objExplorer.Document<\/p>\n<p>Do While True\n    Wscript.Sleep 30000\n    objDoc.Location.Reload(True)\n    If Err &lt;&gt; 0 Then\n        Wscript.Quit\n    End If\nLoop\n<\/PRE>\n<P>The trick here is to use a script to start Internet Explorer, then have that script pass commands to the browser. To that end we begin by adding the <B>On Error Resume Next<\/B> statement (more on that later), and then create an instance of the <B>InternetExplorer.Application<\/B> object. We use the <B>Navigate<\/B> method to open the desired Web page (hey, what a coincidence: it\u2019s the Script Center home page!) and then set the <B>Visible<\/B> property to 1. (Is that important? It is if we actually want to be able to <I>see<\/I> our instance of Internet Explorer.)<\/P>\n<P>Next we use <B>Wscript.Sleep<\/B> to pause the script for 5 seconds (5,000 milliseconds). We do that to ensure that Internet Explorer is fully-loaded before proceeding; if Internet Explorer is <I>not<\/I> completely loaded our next line of code will likely fail, and that means our script will likely fail.<\/P>\n<P>That next line of code, incidentally, creates an object reference to the Internet Explorer document object. That\u2019s needed because, to refresh a Web page, we have to reload the current Internet Explorer document. That\u2019s just the way the Internet Explorer object model works.<\/P>\n<P>After creating this object reference we set up a Do While loop that will run forever. (Don\u2019t worry; there\u2019s an easy way to stop the script.) Inside that loop we call the <B>Sleep<\/B> method and pause the script for 30 seconds; that\u2019s because we decided to refresh the Web page every 30 seconds. What if we wanted to refresh the page every two minutes? No problem; just use this line of code, which pauses the script for 120 seconds (120,000 milliseconds):<\/P><PRE class=\"codeSample\">Wscript.Sleep 120000\n<\/PRE>\n<P>When 30 seconds are up we call the <B>Reload<\/B> method to reload the current document. We pass Reload a single parameter &#8211; <B>True<\/B> &#8211; which tells Internet Explorer to reload the document <I>from the Web server<\/I>. Without this parameter, Reload would reload the document from the Internet Explorer cache, meaning we wouldn\u2019t be getting the latest and greatest version of the Web page.<\/P>\n<P>After calling the Reload method we then check the value of the <B>Err<\/B> object. If an error has occurred (and the prime culprit for that would be the fact that someone has closed browser window) we simply terminate the script. If no error has occurred we loop around, wait for 30 seconds, and then start all over again.<\/P>\n<P>And yes, that <I>is<\/I> the easy way to end the script: just close the browser window. Do that and within 30 seconds the script will terminate. <\/P>\n<P>So there you have it: a script that periodically refreshes a Web page. We\u2019d like to see a Supreme Court nominee (or full-fledged justice, for that matter) write something like <I>that<\/I>.<\/P>\n<P>Well, OK: we mean <I>besides<\/I> Ruth Bader Ginsburg.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I periodically refresh a Web page?&#8212; TZ Hey, TZ. You know, the last couple of weeks have been very difficult for the Scripting Guys; after all, we were positive that one of us would be nominated to become the next Chief Justice of the United States Supreme Court. (We even [&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-68873","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 periodically refresh a Web page?&#8212; TZ Hey, TZ. You know, the last couple of weeks have been very difficult for the Scripting Guys; after all, we were positive that one of us would be nominated to become the next Chief Justice of the United States Supreme Court. (We even [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68873","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=68873"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68873\/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=68873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}