JavaScript:对话框取值、休眠、强制退出链接

来源:互联网 发布:mac pro cpu型号 编辑:程序博客网 时间:2024/04/27 22:51
 

JavaScript:对话框取值、休眠、强制退出链接

近日,遇到一个问题,有的时候需要在页面中有单击行为的地方,打开一个新的窗体,然后经过操作后,获取所要生成的数据并正常关闭打开的窗体。前些地方,看到一个方法,在父窗体上打开子窗体,然后在子窗体的某个事件中,直接设置父窗体的某个控件的值,达到操作的目的。但这样做不够灵活,不够通用化。

于是,我参考了只有IE支持的模态窗口,关闭会返回子窗体的window.returnValue。当然没有设置会返回null也就是undefined。我设计了getDialog函数,用于获取子窗体的返回值,异常或者无效将返回空否则为正常返回数据。而原先的window.open我也改进一下设计了getWindowReturnValue,通过侦听子窗体的window.returnValue,如果有数据将开始终止。如果窗体仍然打开则自动关闭。同样两个函数都需要进行正确的设置window.returnValue。为此,我考虑今后将支持子窗体的某个函数来返回自定义的数据。

而在主窗体中关闭非正常退出的执行退出程序的函数onClearSession需要挂接在onUnload事件中,执行集中处理退出事件的页面,比如logout.jsp。代码主要思想是获取用户直接点标题栏按钮或菜单还有用快捷关闭,然后在执行相应的代码。onUnload在页面刷新或者跳转、前进、后退都会发生,必须要判断出符合条件的情形,而这些参考了一些资料。值得一提的是必须要经过休眠,再进行关闭调用退出窗体。否则,代码没有执行完毕就关闭窗体,不能保证代码有效执行。同时需要说明,清除Session连接及相关资源的页面,代码需要在onBeforeUnload而不是onUnload。两者执行的生命周期不同。

相关内容可以参考技术版块200751617日发布的关于清除Session的两篇日志。

//IE中打开模态对话框并返回数据,建议在按钮或者窗口关闭事件前返回

  //需要在打开页面设置window.returnValue

  //错误或无效返回空

  function getDialog(url)

  {

     var returnValue;

     try

     {

        returnValue=window.showModalDialog(url);   

     }catch(e)

     {

          return "";

     }   

     return (returnValue==null||returnValue=="undefined")?"":returnValue;

  }

  //使用window.open来获取子窗口数据,需要动态设置window.returnValue

  //参数为:地址、名称、宽度、高度,不设置宽度与高度请设为0

  function getWindowReturnValue(url,name,width,height)

  {

      var opener=null;

      var spec="toolbar=0,menubar=0,scrollbars=0,resizeable=0";

      var returnValue=null;

      if(width>0) spec=spec+",width="+String.valueOf(width);

      if(height>0)

      {

          spec=spec+",height="+String.valueOf(height);

      }

     opener=window.open(url,name,spec);

      

      while(!opener.window.closed)

      {

          returnValue=opener.window.returnValue;

          if(returnValue!=null&&returnValue.length>0) break;

      }

      if(!opener.window.closed) opener.window.close();

      return returnValue;   

  }

//用户IE非法关闭窗口清除会话,将会话清除程序做在一个URL地址中。在主窗体嵌入的onUnload事件嵌入该事件即可

  function onClearSession(clearSessionUrl)

  {

      var newWindow;

     if((window.screenLeft>=10000 && window.screenTop>=10000)||event.altKey)

     {  

        newWindow=window.open(clearSessionUrl,

'退出程序,'width=0,height=0,top=4000,left=4000');//新窗口将在视区之外打开

        newWindow.opener=null;

        sleep(5000);

        newWindow.close();//新窗口关闭

     }   

 }

//休眠函数:参数为毫秒

function sleep(milisecond)

{

   var currentDate,beginDate=new Date();

   var beginHour,beginMinute,beginSecond,beginMs;

   var hourGaps,minuteGaps,secondGaps,msGaps,gaps;

   beginHour=beginDate.getHours();

   beginMinute=beginDate.getMinutes();

   beginSecond=beginDate.getSeconds();

   beginMs=beginDate.getMilliseconds();

   do

   {

      currentDate=new Date();

      hourGaps=currentDate.getHours() - beginHour;

      minuteGaps=currentDate.getMinutes() - beginMinute;

      secondGaps=currentDate.getSeconds() - beginSecond;

      msGaps=currentDate.getMilliseconds() - beginMs;     

      if(hourGaps<0)   hourGaps+=24;   //考虑进时进分进秒的特殊情况

      gaps=hourGaps*3600+ minuteGaps*60+ secondGaps;

      gaps=gaps*1000+msGaps;

   }while(gaps<milisecond);  

}

原创粉丝点击