How Can I Periodically Refresh a Web Page?

How Can I Periodically Refresh a Web Page?

  • Comments 23
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I periodically refresh a Web page?

-- TZ

SpacerHey, Scripting Guy! AnswerScript Center

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.Navigate "http://www.microsoft.com/technet/scriptcenter"   
objExplorer.Visible = 1

Wscript.Sleep 5000

Set objDoc = objExplorer.Document

Do While True
    Wscript.Sleep 30000
    objDoc.Location.Reload(True)
    If Err <> 0 Then
        Wscript.Quit
    End If
Loop

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):

Wscript.Sleep 120000

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.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • doesnt work for me.

    i have simply copy pasted this code and just changed the url, though it opens the url it neither refreshes nor does the script terminate if i close the ie window.

    where could the problem be??

  • ...what I need is code that refreshes a URL if it is already open. I found another script that does this successfully using Shell and ShellWindows objects, but not when logged on as the system user. Presumably, this is because the system user has no UI shell.

  • Seems to work for me...but what do I need to refresh an IE page running multiple tabs (ie 7): EX:  www.google.com www.yahoo.com www.msn.com (just for example purposes). Thanks!

  • works fine. thanks.

  • How do I run this script as a BAT file using notepad to write it?

  • Hello! Has anyone figured out how to refresh an IE page running multiple tabs? (ie 7): EX:  www.google.com www.yahoo.com www.msn.com (just for example purposes). Thanks!

  • Awesome! Exactly what I needed. To the BAT file question..... try renaming it to refresh.VBS since it's a VBScript and not a batch command script.

    Scripting Guy, I would totally vote for you for Chief Justice. I wonder if the Weiner position will open up for me any time soon.

  • I used the above and created a VBS script; works great.  How can I add a key press (or better yet a left mouse click) at the mouse's current position before the refresh command??

    I've tried turning on the Mouse Keys in the handicap accessibility settings, to simply press the numkey 5 but i am having difficulty with this.

  • Where do I put this script so it will execute?

  • It works fine. Thank you so much!!!

  • @Chryssa Thanks for the feedback. I am glad you found the script useful, and that it helped you.

  • Where is everyone else locating this script for it to execute?

  • The script works but when it refreshes my site a pop appears for me to click ok. How could I add that to the script to auto click ok when the page refreshes?

    HELP!!!!

  • request please, can you make internet explorer minimise upon opening? as i use this during a presentation and i need it hidden at all times.

    Also will the script perodically refresh only if the window is selected and not if its minimised?

  • i need to open it in google chrome not internet explorer