簡體   English   中英

JavaScript jQuery調用php無法在iPhone / Ipad上運行

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

我今天一直在研究以下代碼,並且在我的本地計算機上運行良好。 但是,當我在iPhone / iPad上對其進行測試時,它根本無法工作。

該代碼應該在用戶離開網站時將頁面訪問的持續時間保存到文本文件中。 我知道可以從Internet上下載很多其他腳本,但是我想自己寫。 歡迎任何建議。

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); 
 ?>

iOS不支持onbeforeunload 這個問題解決了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM