简体   繁体   English

JavaScript jQuery调用php无法在iPhone / Ipad上运行

[英]javascript jquery calling php not working on iPhone/Ipad

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. 但是,当我在iPhone / iPad上对其进行测试时,它根本无法工作。

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. 我知道可以从Internet上下载很多其他脚本,但是我想自己写。 Any suggestions are welcome. 欢迎任何建议。

index.php: 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: 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); 
 ?>

iOS doesn't support onbeforeunload . iOS不支持onbeforeunload This question addresses this issue. 这个问题解决了这个问题。

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

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