Reputation: 15
I've been working on the following code today and it works quite nicely on my local machine. But when I test it on my iPhone/iPad it doesn't work at all.
The code is supposed to save the duration of the page visit to a text file when the user leaves the website. I know there are a lot of alternative scripts for download on the internet, but I want to write this myself. Any suggestions are welcome.
index.php:
<!DOCTYPE html>
<html>
<HEAD>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
function unloadPage(){
var dd = new Date();
var hh = (dd.getHours()<10?'0':'') + dd.getHours();
var mm = (dd.getMinutes()<10?'0':'') + dd.getMinutes();
var ss = (dd.getSeconds()<10?'0':'') + dd.getSeconds();
var htmlString="<?php echo date('Gis') ?>";
var timed = (hh + "" + mm + "" + ss) - htmlString;
jQuery.ajax({
url: 'time2.php?t=' + timed,
success: function(result) {
if(result.isOk == false)
alert(result.message);
},
async: false
});
}
window.onbeforeunload = unloadPage;
</script>
<meta charset="utf-8">
<title>thepaintjob.nl</title>
</head>
<BODY>
<?
$myFile = "temp.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 5);
fclose($fh);
echo duration($theData);
?>
</body>
</html>
<?php
function duration($secs)
{
$vals = array('w' => (int) ($secs / 86400 / 7),
'd' => $secs / 86400 % 7,
'h' => $secs / 3600 % 24,
'm' => $secs / 60 % 60,
's' => $secs % 60);
$ret = array();
$added = false;
foreach ($vals as $k => $v) {
if ($v > 0 || $added) {
$added = true;
$ret[] = $v . $k;
}
}
return join(' ', $ret);
}
?>
time2.php:
<?php
$myFile = "temp.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 5);
fclose($fh);
echo $total = $theData+$_GET['t'];
$File = "temp.txt";
$Handle = fopen($File, 'w');
$Data = $total;
fwrite($Handle, $Data);
fclose($Handle);
?>
Upvotes: 0
Views: 401
Reputation: 2547
iOS doesn't support onbeforeunload
. This question addresses this issue.
Upvotes: 2