关闭网页时请求一个URL
时间:2009-04-26
来源:互联网
这个问题应该和JQuery无关,但还是请这里的高手帮忙解答一下。
我想在页面关闭的时候执行一个URL,以便作一些处理。但是我在body的onunload和onbeforeunload中写了代码,好像都不能访问。代码里面用了$,ajax({url:'xxx',async:false}) 也不行。$.get('xxxxx')也不行。
实际上我是在一个弹出窗口关闭的时候执行这个动作的,我如果用opener.location.href='xxxx'就可以。但是这样一来,如果用户关闭了opener,就不起作用了。
请问有无好的解决办法?
另外,是不是jquery不支持onbeforeunload事件?
多谢。
我想在页面关闭的时候执行一个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');
这样可以实现。。。
我在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);
}
});
});
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
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28