+ -
当前位置:首页 → 问答吧 → 关闭网页时请求一个URL

关闭网页时请求一个URL

时间:2009-04-26

来源:互联网

这个问题应该和JQuery无关,但还是请这里的高手帮忙解答一下。

我想在页面关闭的时候执行一个URL,以便作一些处理。但是我在body的onunload和onbeforeunload中写了代码,好像都不能访问。代码里面用了$,ajax({url:'xxx',async:false}) 也不行。$.get('xxxxx')也不行。

实际上我是在一个弹出窗口关闭的时候执行这个动作的,我如果用opener.location.href='xxxx'就可以。但是这样一来,如果用户关闭了opener,就不起作用了。

请问有无好的解决办法?

另外,是不是jquery不支持onbeforeunload事件?



多谢。

作者: benlaohu   发布时间: 2009-04-26

你为啥想要在页面关闭的时候去 ajax 发起一个请求呢?
为什么不在页面打开时去请求?

换一种思维方式有时也可以达到同样的目的的..

作者: kuco   发布时间: 2009-04-26

这个没有办法去换,只有在关闭网页的时候才能触发这个事件。

我在onbeforeunload里面用这个:
window.location.href='xxxx';
alert('xxxxx');

这样可以实现。。。

作者: benlaohu   发布时间: 2009-04-26

这个问题,我以前也碰到进,得加一个延时,才行
    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);

}

  $(document).ready(function(){
  $(window).unload(function(){

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

       $.get("SessionClear.aspx");

   sleep(8000);
  }

  });

  });

作者: seekarmor   发布时间: 2009-04-26

相关阅读 更多

热门下载

更多