避免 session timeout by jquery.

  • 3421
  • 0

摘要:避免 session timeout by jquery.

原理:由隨便一個框 , 去改寫所有框架的 keypress 事件 , 如果有 keypress 事件 , 改變 ajax 呼叫旗標 ,
     定期以 ajax 回 call server 端程式 , 避免逾時 , 多 keypress 處理是避免 , 使用者真的都沒任何
     動作 , 系統也不作自動登出的風險。

01 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
02 <html xmlns="http://www.w3.org/1999/xhtml">
03 <head>
04 <meta http-equiv="Content-Type" content="text/html; charset=big5" />
05 <title> 上框架 </title>
06 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js" type="text/javascript"></script>
07 <script language="javascript" type="text/javascript">
08  var flg_go_ajax = false;
09
10  function do_ajax(o) {
11      ReDf(top);
12      if (flg_go_ajax)
13    $.get('/test.asp',{'p':'^^'},function(data){
14        flg_go_ajax = false;
15        $('#span_msg').html($('#span_msg').html()+'.');
16    });
17  }
18
19  function ReDf ( o ) {
20      //try {
21    for ( var i = 0 ; i < o.frames.length ; i++ ) {
22        if ( o.frames[i] != self ) {
23      var _doc = o.frames[i].document;
24      if ( _doc.flg_set_tg != 1 ) {
25          _doc.flg_set_tg = 1;
26          $(_doc).ready(
27        function(){
28            $(_doc).keypress(function(){flg_go_ajax = true;});
29        }
30          );
31      }
32      ReDf ( o.frames[i] );
33        }
34    }
35      //} catch(e) { }
36  }
37  $(document).ready(function(){do_ajax(top);});
38  var time_id = setInterval( "do_ajax(top)" , 600*1000 ); //十分鐘回 call一次
39 </script>
40 </head>
41 <body>
42  <form>
43      <span id="span_msg">#</span><br/>
44      <a href="b1.htm" target="main">表單一</A>   
45      <a href="b2.htm" target="main">表單二</A>
46  </form>
47 </body>
48 </html>
49