Learn WordPress
Home » Script

How To Auto Refresh Your Web Pages

auto refresh

If you are looking for auto refresh for your web page or blog page this is an easy script that you can use to realized your pages.

Auto refresh is needed if your web/blog have high traffic and high new post content. I decided on a JavaScript, you can also refresh a page with a meta tag, I’ll show details as well.

Use this syntax, within the <HEAD> section of the document:

  1. Details on JavaScript Refresh
    JavaScript is pretty straight. On the pre-render of the repeater I add the JavaScript to the link button:

    protected void rFiles_preRender(object sender, EventArgs e)
    {
    LinkButton lbtn = null;
    foreach (RepeaterItem ri in rpFiles.Items)
    {
    if (ri.ItemType == ListItemType.Item ||
    ri.ItemType == ListItemType.AlternatingItem)
    {
    lbtn = (LinkButton)ri.FindControl(“lbtnFile”);
    if (lbtn != null)
    { //reload the page in 1 second.

    lbtn.Attributes.Add(“onclick”, “setReloadTime(1)”);
    }
    }
    } //foreach

    }

    The parameters allows the JavaScript to run before my postback which does the server transfer. JavaScript function looks like this:

    <script type=”text/javascript” language=”javascript”>
    var reloadTimer = null;
    var sURL = unescape(window.location.pathname);

    function setReloadTime(secs)
    { //this function is used to refresh the broswer

    if (arguments.length == 1)
    { //if some seconds are passed in then create and set the timer and

    //have it call this function again with no seconds passed in

    if (reloadTimer) clearTimeout(reloadTimer);
    reloadTimer = setTimeout(“setReloadTime()”,
    Math.ceil(parseFloat(secs) * 1000));
    }
    else
    { //No seconds were passed in the timer must be up clear the timer

    //and refresh the browser

    reloadTimer = null;
    //passing true causes the request to go back to the web server

    // false refreshs the page from history

    //This is javascript 1.2

    location.reload(true);
    //This is javascript 1.1

    window.location.replace( sURL );
    }
    }
    </script>

    Some on the Meta Tag Refresh
    The Meta tag Refresh has been roughly for a while. Frequently you see it once an old web page has went to afresh web page. You normally put the meta tag in the header of your page. The syntax is:

    <meta http-equiv=”Refresh” content=”n;url”/>

    n is the number of seconds, url is the url to refresh to. And if you leave the url off then the page refreshes itself.

    These’s my sample code and the page updates time in every 5 seconds. The pageload event I have this code to set the meta tag:

    if (this.Master.Page.Header != null)
    {
    HtmlHead hh = this.Master.Page.Header;
    HtmlMeta hm = new HtmlMeta();
    hm.Attributes.Add(“http-equiv”, “Refresh”);
    hm.Attributes.Add(“content”, “5”);
    hh.Controls.Add(hm);
    }

  2. Another Simple Script Auto Refresh Your Web Pages.
    These’s another my sample simple code script auto refresh using javascript for your web pages and the page updates time in every 60 seconds/1 minute.

    <script type=’text/javascript’>
    // The time out value is set to be 60,000 milli-seconds (or 60 seconds)
    setTimeout(' document.location=document.location' ,60000);
    </script>

FREE Cloud Storage