简体   繁体   English

没有运行onbeforeunload的Javascript代码

[英]Javascript code not running onbeforeunload

I have 3 variables to track timespent in the site 我有3个变量来跟踪网站中的timepent

 $_SESSION['checkin']; //logged when user first enters the site 
 $_SESSION['loggedAt']; //logged when the user leaves the site
 $_SESSION['timespent']; //time spent in minutes

What I'm trying to do is everytime the user leaves the page, these values are pushed into the database. 我想要做的是每次用户离开页面时,这些值都会被推送到数据库中。 What I've done is use onbeforeunload. 我所做的是使用onbeforeunload。

Below is how I'm using it: 以下是我使用它的方式:

On every page: 在每个页面上:

 <script type="text/javascript" src="js/storeSession.js"></script>

storeSession.js: storeSession.js:

window.onbeforeunload = confirmExit();
  function confirmExit()
  {
        var xhr = new XMLHttpRequest();
        xhr.open('post','../php/storeSession.php',true);
        xhr.send();
        return "Are you sure you want to leave?";
  }

And storeSession.php: 和storeSession.php:

$_SESSION['loggedAt'] = time();
$temptime = (double)$_SESSION['timespent']/60;
$_SESSION['timespent'] = $_SESSION['loggedAt'] - $_SESSION['checkin'];

mysql_select_db($db, $conn) or die(mysql_error()); 

$data = mysql_query("INSERT INTO user (user_id, timespent) VALUES ($tempuser, $temptime) ON DUPLICATE KEY UPDATE timespent = timespent + $temptime")
        or die(mysql_error()); 

$_SESSION['timespent'] = null;
$_SESSION['loggedAt'] = null;
$_SESSION['checkin'] = null;

mysql_close($con);

I can't find the problem and everything seems to be working alright (no errors and such). 我找不到问题,一切似乎都运转正常(没有错误等)。 Does anyone have any idea? 有人有什么主意吗?

One problem is this line: 一个问题是这一行:

window.onbeforeunload = confirmExit();

You called the function and assigned the return value to window.onbeforeunload , instead of assigning the function itself. 您调用了该函数并将返回值分配给window.onbeforeunload ,而不是分配函数本身。

Change the line to: 将行更改为:

window.onbeforeunload = confirmExit;

...or use a closure. ......或使用封闭物。

Also you are making a POST request without sending any data (you didn't pass anything to the first argument of send() ) - you probably want to be making a GET request instead. 你也在发送POST请求而不发送任何数据(你没有传递任何东西到send()的第一个参数) - 你可能想要发出一个GET请求。

You need to remove the parenthese from your onbefore function 您需要从onbefore函数中删除括号

window.onbeforeunload = confirmExit;

Also you are ending the session with the XHR call, before the confirm. 您还要在确认之前结束与XHR呼叫的会话。 If the users cancels the unload, he is still logged out. 如果用户取消卸载,他仍然会被注销。

Have you tried using onunload instead of onbeforeunload . 您是否尝试过使用onunload而不是onbeforeunload I remember that the latter one is restricted because it could potentially be used to prevent users from exiting page (or even the browser). 我记得后者是受限制的,因为它可能会被用来阻止用户退出页面(甚至是浏览器)。 I am not completly sure, but i think that (most) browsers only read the returned string and ignore the rest. 我不完全确定,但我认为(大多数)浏览器只读取返回的字符串而忽略其余的。

Also, i have had some problems with on(before)unload actions myself, and solved them by explicitly using synchronous communication with the server (i used jquery + ajax), otherwise the ajax request wasn't completed (though I have no experience with XMLHttpRequest , but i think this option should be comparable) 另外,我自己在on(之前)卸载操作时遇到了一些问题,并通过显式使用与服务器的同步通信解决了这些问题(我使用了jquery + ajax),否则ajax请求没有完成(尽管我没有经验) XMLHttpRequest ,但我认为这个选项应该具有可比性)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM