0

Here is my Javascript Function below. Is to possible to have an alert overwrite another alert. For instance alert(message); triggers first but if session expires it triggers alert("Session expired. You will be redirected to login page");. The only way you will see the second alert is if you click ok on the alert(message). Is it possible for the second alert to just overwrite the first alert if session expires or any other way of doing this.

<!-- Session Timeout Function-->
  <script language="javascript" type="text/javascript">
  var sessionTimeout = <%= Session.Timeout %>;
  var sessionTimeoutWarning = sessionTimeout - 1;
  var timeOnPageLoad = new Date();
  var warning = null;
  var timeout = null;

  if ( <% if (MasterPageTemplate.Classes.CmwSession.IsAuthenticated) Response.Write("1"); else Response.Write("0");  %> == 1)
  {
     //For warning
     warning = setTimeout('SessionWarning()', parseInt(sessionTimeoutWarning) * 60 * 1000);
     //To redirect to the welcome page
     timeout = setTimeout('RedirectToWelcomePage()',parseInt(sessionTimeout) * 60 * 1000);
   }
    //Session Warning
    function SessionWarning() {
        //minutes left for expiry
        var minutesForExpiry =  (parseInt(sessionTimeout) - parseInt(sessionTimeoutWarning));
        var message = "Your session will expire in another " + minutesForExpiry + 
        " minutes! Please Save the data before the session expires";
        alert(message);


        var currentTime = new Date();
        //time for expiry
        var timeForExpiry = timeOnPageLoad.setMinutes(timeOnPageLoad.getMinutes() + parseInt(sessionTimeout)); 

        //Current time is greater than the expiry time
        if(Date.parse(currentTime) > timeForExpiry)
        {
            alert("Session expired. You will be redirected to login page");
            window.location = "Default.aspx";
        }
        else
        {
           $.ajax({
                type: "POST",
                url: 'Default.aspx/PingSession',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                type: "POST",
                success: function (msg) {
                alert("Your session is now valid.")
                }
         });

         if (warning != null)
         {
            clearTimeout(warning);
            //For warning
            warning = setTimeout('SessionWarning()', parseInt(sessionTimeoutWarning) * 60 * 1000);
         }

         if(timeout != null)
         {
           clearTimeout(timeout);
           //To redirect to the welcome page
           timeout = setTimeout('RedirectToWelcomePage()',parseInt(sessionTimeout) * 60 * 1000);
         }
        }
    }

    //Session timeout
    function RedirectToWelcomePage(){
        alert("Session expired. You will be redirected to login page");
        window.location = "Default.aspx";
    }

I4V
  • 34,891
  • 6
  • 67
  • 79

1 Answers1

1

This is not possible. You do not have any control over the dialog box after it has been opened. As this answer suggests you could try a modal dialog using a UI framework: Javascript close alert box

Community
  • 1
  • 1
FakeTruth
  • 135
  • 5
  • that's the route i was thinking if this didn't work, but i wanted to see if it was possible to overwrite the alert box – ChelseaBlue1 Sep 20 '13 at 20:20