Hey, Scripting Guy! How can I periodically refresh a Web page?
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 bought the robes and everything, robes which - for reasons unknown - Peter won’t let us return.) Instead, we’re forced to sit and watch John Roberts answer questions about strict constructionism and the applicability of legal precedent. Talk about easy! We’d like to see John Roberts answer a question like “How can I periodically refresh a Web page?”
Oh, right: you’d probably like to see us answer a question like “How can I periodically refresh a Web page?” Okey-doke:
On Error Resume Next
Set objExplorer = CreateObject("InternetExplorer.Application")
objExplorer.Visible = 1
Set objDoc = objExplorer.Document
Do While True
If Err <> 0 Then
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 On Error Resume Next statement (more on that later), and then create an instance of the InternetExplorer.Application object. We use the Navigate method to open the desired Web page (hey, what a coincidence: it’s the Script Center home page!) and then set the Visible property to 1. (Is that important? It is if we actually want to be able to see our instance of Internet Explorer.)
Next we use Wscript.Sleep 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 not completely loaded our next line of code will likely fail, and that means our script will likely fail.
That next line of code, incidentally, creates an object reference to the Internet Explorer document object. That’s needed because, to refresh a Web page, we have to reload the current Internet Explorer document. That’s just the way the Internet Explorer object model works.
After creating this object reference we set up a Do While loop that will run forever. (Don’t worry; there’s an easy way to stop the script.) Inside that loop we call the Sleep method and pause the script for 30 seconds; that’s 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):
When 30 seconds are up we call the Reload method to reload the current document. We pass Reload a single parameter - True - which tells Internet Explorer to reload the document from the Web server. Without this parameter, Reload would reload the document from the Internet Explorer cache, meaning we wouldn’t be getting the latest and greatest version of the Web page.
After calling the Reload method we then check the value of the Err 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.
And yes, that is the easy way to end the script: just close the browser window. Do that and within 30 seconds the script will terminate.
So there you have it: a script that periodically refreshes a Web page. We’d like to see a Supreme Court nominee (or full-fledged justice, for that matter) write something like that.
Well, OK: we mean besides Ruth Bader Ginsburg.