Tuesday, May 14, 2013

Button Click Refresh Page, Refresh Page Timer, Call Function Timer using Javascript in Asp.net

Hello in this post i will show how to refresh a page in JavaScript.

1. Refresh Page on Button Click :

Call this below function on button click event, This will refresh the page.


 function RefreshFunction() {
        document.location.reload(true);
    }

2. Refresh Page on Timer

Here we will specify the time interval. As per the specified time interval, the page will automatically get refreshed.

setTimeout("location.reload(true);", 4000);

//4000 is 4 seconds.

3. Call a javascript function on regular Intervals

Here we will call a JavaScript function as per the specified time interval.


function RefreshFunction2() {
        setTimeout(viewDate(), 2000);
    }

    function viewDate() {
        var todayDate = new Date();
        var DateToday = todayDate.getDate()
        alert(todayDate);
     }


After every 2 seconds viewDate() function will get called.


Example:


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">

        function RefreshFunction() {
            document.location.reload(true);
        }

        function RefreshFunction2() {

            setTimeout(viewDate(), 2000);
        }

        function viewDate() {
            var todayDate = new Date();
            var DateToday = todayDate.getDate()
            alert(todayDate);
        }

        alert('Hello World');
        setTimeout("location.reload(true);", 4000);


</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="RefreshFunction();" />
        <asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="RefreshFunction2();" />
        
    </div>
    </form>
</body>
</html>



No comments:

Post a Comment