How Can I Periodically Refresh a Web Page?

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 “”
objExplorer.Visible = 1

Wscript.Sleep 5000

Set objDoc = objExplorer.Document

Do While True
Wscript.Sleep 30000
If Err <> 0 Then
End If

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.

Comments (29)

  1. mredwilson says:

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

  2. Anonymous says:

    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??

  3. Interesting, but... says:

    …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.

  4. Dan_IT says:

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

  5. V says:

    works fine. thanks.

  6. jamie says:

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

  7. jadielmota says:

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

  8. Change says:

    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.

  9. Keith says:

    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.

  10. Great, one question about this says:

    Where do I put this script so it will execute?

  11. Chryssa says:

    It works fine. Thank you so much!!!

  12. Where to put script says:

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

  13. Script works great but... says:

    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?


  14. alex says:

    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?

  15. how do i open it in different browser says:

    i need to open it in google chrome not internet explorer

  16. You are very clever... says:

    Just what I needed – I was killing IE process and starting it again which was messy! Thanks : )

  17. paul says:

    This works superbly but a much needed feature is how can i set the script to end? For instance say i am taking screenshots of weather webcam or a ships passage at sea i may only want it to run for a specified number of hours.

    I've no idea of the coding syntax used here (only know website php, css, java etc) but i imaginge it's fairly simple? Most likely using 'End If' or 'End' when time elapsed by milliseconds?

  18. paul says:

    Just to clarify the above. I won't be around to close the browser and i'm sure millions of scripts don't need someone to physically press a button to end them.

    I've already got it working, opening up IE to the selected page and then taking and saving screenshots at set intervals, however, i really need to be able to time the script to stop at a set elapsed time. Possibly even a specified time using the computer clock too.

  19. jrv says:

    @Paul – A blog is not really the best place to ask complex scripting "how-to" questions.  Try posting in the forum.  I am sure someone will be willing to work with you on a fix for you issue.

    The link to the forum is on the rights side of this page along with many resource links with help and examples.  You might find them useful as you learn to script.

  20. paul says:

    Sorry jrv, i hadn't seen the link to a forum, or even glanced at any of the left hand side of the page!, as i was wrapped up in the article and responses. Also, prior comments had included questions and requests so i didn't realize. Thanks for the heads up as to where to head for advice.

  21. Rick says:

    This works for web pages on the internet. I need it to refresh a local html file on my computer. Is this possible?

  22. JV says:

    objExplorer.Navigate "c:myfile.htm""

  23. MJ says:


    This is fantastic! is there a way to refresh the page to the homepage rather than the current page?

  24. Yosef says:

    Hey, Scripting Guy! How can I periodically refresh a Web page using https://

  25. tom says:

    Does this .vbs script need to be located in a particular place? It runs for me, but apparently fails when checking the objdoc. If I run in command prompt, the cmd window closes at the point it should be refreshing the page.

  26. santhosh says:

    How to use the same script to refresh a page in firefox?

  27. santhosh says:

    Some one please help me.. 🙁
    how to use similar script to refresh a web page in firefox ??

  28. bknight says:

    Someone had asked to do this task with a web page that was already opened, my case exactly. Is there a way to write a batch file for this type?

  29. TerryF says:

    This code works until closed. I modified it so that it runs in a loop, but I would like to run the routine until a time like 1500(my machine time zone, how could this be modified?
    On Error Resume Next
    Set IE = CreateObject("InternetExplorer.Application")
    With IE
    .Navigate ""
    .Visible = True
    End With
    counter = 1
    Do Until counter>= 181
    WScript.Sleep 180000 ‘3 min
    counter = counter + 1
    The above was intended to run for 9 hours, but ends after about 6 hours