This discussion is archived
3 Replies Latest reply: Feb 1, 2013 1:28 AM by OliverL RSS

Error Handling/Session Timeout with APEX & Ajax / Javascript

OliverL Newbie
Currently Being Moderated
In our project we have the problem, that a session timeout can happen when the user remains on the same page and a jQuery/JavaScript/ajax-process is used.

If an action calls a ajax process without an active session is calling within a popup the html of the login page is show to the user.

How could an error handling be implemented that no html will be shown in the popup?


Further on we have external JavaScript-Files which need to read the content of Application Items.
Is there any elegant/small/reuseable way to get the value of the application Items?
The &APP_ID. Syntax doesn't work in external JS-Files.
  • 1. Re: Error Handling/Session Timeout with APEX & Ajax / Javascript
    VC Guru
    Currently Being Moderated
    Oliver L wrote:
    Further on we have external JavaScript-Files which need to read the content of Application Items.
    Why?
    Is there any elegant/small/reuseable way to get the value of the application Items?
    Application items are supposed to be server side variables so why do you want to expose to the client, instead use hidden items on page 0 which can be accessed using *$v* syntax
    See http://docs.oracle.com/cd/E37097_01/doc/doc.42/e35127/javascript_api.htm#CDEEIGFH
    The &APP_ID. Syntax doesn't work in external JS-Files.
    This is not an application item, its a built-in. you can access the app id using $v('pFlowId') anywhere
  • 2. Re: Error Handling/Session Timeout with APEX & Ajax / Javascript
    48920 Explorer
    Currently Being Moderated
    Hi Oliver

    I personally think if you get a timeout that the popup is closed and your parent page is redirected to the login page. Using a check in success/error callback in an AJAX call you inspect the xhr.responseText to work out if you have received a timeout would be by recommendation and if found close the popup and set window.location to the login page.

    However for non AJAX calls you will face a problem. In this situation you need a script in your login page which checks for window.opener and if so closes the popup and sets the opener window.location to your login page. e.g. I would add something like this to your login page header, or login page template header.
      <script>
        if (window.opener) {
          window.opener.location = "&LOGOUT_URL.";
          window.close();
        }
      </script>
    Note: tested on FF only. Plus the above javascript is just a very basic example and will close any popup that shows the login page and will reload the parent with the login page. It can lead to undesired functionality for web pages which open your application in a popup, since it will automatically close and reload the calling page which might frustrate people. In this case you need to add more conditional logic in the script to check the current window.opener.location to check if the URL is for your application then perform the close and reload action.

    Of course there are other ways to handle what happens this is just an option I would explore.

    If you need to read values of application items not page items in javascript you need to add some script to your page FOOTER which defines these e.g.
    <script>
    apex.appItem = (function () {
       var appItems = {};
       appItems.MY_APP_ITEM1 = "&MY_APP_ITEM1.";
       appItems.MY_APP_ITEM2 = "&MY_APP_ITEM2.";
       return function (item) {
          return appItems[item];
       }
    })();
    </script>
    To get the value of an applciation item:
    apex.appItem('MY_APP_ITEM1');
    In the above script the application item values are secured from being changed since they can only be accessed through the function as they are defined as local variables within. The only issue you will face is accessing the value of an application item that's been updated in session state by an AJAX process. Using local values printed on page render like this can get out of sync. If your applciation item values include line breaks or double quotes then you need to escape these, if you need that support use a PLSQL page region and output the script after JSON escaping the item values.

    In the example I use the apex Namespace, some people may argue this is bad practice, but for me I like keeping everything apex related together. There's pro's/cons to the argument so the decision is yours.

    Note: the reason for adding the script to your footer is to make sure the application item values have been set in session state before they are outputted.

    The previous post from VC is the right way to access input page items and DOM values.

    Cheers
    Matt
  • 3. Re: Error Handling/Session Timeout with APEX & Ajax / Javascript
    OliverL Newbie
    Currently Being Moderated
    Thanks Matt for your great answer.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points