Sample Inject JavaScript Policy

The script in this example assumes that the contract timeout is five minutes. After four minutes, the user gets a popup message (timeout.html) with an option to refresh the page. If the user clicks on the option, the session is extended. If the user closes the popup or does not respond to the message, the system executes AGLogout and the session gets terminated.

Perform the following steps:

  1. Configure a Form Fill policy to insert Java Script into the head block of an HTML page.

    1. Go to Policies > New.

    2. Specify a name for the policy and select the type as Access Gateway: Form Fill.

    3. In the Actions section, click New and then select Inject JavaScript.

    4. Define a CGI matching Criteria or Page Matching Criteria.

    5. Under the Configure Javascript section, select In Head Block.

    6. Click Configure JavaScript and copy the following script:

      <script language="JavaScript">
      var x; 
      var timerID ;
      
      function timeoutClock()
      {
      if(x==60) // 60 seconds is the session time left when the session expire warning message appears. 
      { 
      newwindow = window.open('timeout.html','toWindow','toolbar=no,menubar=no,resizable=no,scrollbars=no,status=no,location=no,width=300,height=200'); 
      }
      if(x==0)
      {
      window.location.href = 'https://www.ag1.com:443/AGLogout' // AGLogout link. 
      }
      x=x-1;
      
      var t=setTimeout(function() {timeoutClock()} ,1000);
      
      }
      function resetClock()
      {
      clearTimeout(timerID);
      x = 300; //5 Minutes. This is the contact timeout defined.
      timeoutClock();
      }
      </script>

      In this script, set the value of x inside the function resetClock() according to the contract time out. In the example, x is set 300 that is equivalent to five minutes. Also, modify the following link in the script based on your configuration. This is a simple AGLogout link.

      window.location.href = 'https://www.ag1.com:443/AGLogout'
    7. Click OK > Apply Changes.

  2. Assign the policy to a protected resource.

    For more information, see Assigning a Form Fill Policy to a Protected Resource.

  3. Ensure to call the resetClock() function in the body tag of the HTML page (<body onload=resetClock();>). This initializes the counter to 300 every time the page is loaded.

  4. Create a timeout.html page, which contains warning message for the user that the session is going to end soon. The content of timeout.html can be as follows:

    <script type="text/javascript">
    var howLong = 60000;
    function closeMe()
    {
    var t = setTimeout(function() {self.close()},howLong);
    }
    function closeCurrentWindow()
    {
      window.close();
    }
    </script>
    Click <a href="javascript:window.opener.location.href = window.opener.location.href;window.close()";>[here]</a>to refresh now.
    <br>Else you will be logged out in 60 seconds
    <body onload=closeMe();
    Timeout
    </body>