2013-08-02 33 views
0

我有記錄訪問時間的問題。簡單的訪問時間記錄器與AJAX

我寫了測試的html文件是這樣的:

<!DOCTYPE html> 
<html> 
    <body> 
     <script language="JavaScript" type="text/javascript"> 

      function enter() { 
       this.chrono = new Date().getMilliseconds(); 
       alert("test"); 
      } 

      function leave() { 
       this.chrono = new Date().getMilliseconds() - this.chrono; 

       var myAjax = new Ajax.Request('visitor_log/ajax_store_visit_duration.php?visit_duration=' + this.chrono.toString(),{ 
         method: 'get', 
         onComplete:handlerFunction 
       }); 

       return null; 
      } 

      window.onload = enter; 
      window.onbeforeunload = leave; 
     </script> 
    </body> 
</html> 

PHP文件(visitor_log/ajax_store_visit_duration.php):

<?php 

if(isset($_GET["visit_duration"])) 
{ 
    $text = $_GET["visit_duration"]; 

    log($text); 
} 
else die("error"); 

function log($text) 
{ 
    $myFile = "test.txt"; 
    $fh = fopen($myFile, 'wb'); 
    fwrite($fh, $text); 
    fclose($fh); 
} 

?> 

當我在瀏覽器中鍵入:

http://localhost/visitor_log/ajax_store_visit_duration.php?visit_duration=123

它創建了我想要的文本文件,但似乎AJAX調用onbeforeunload事件不起作用。

我的代碼有什麼問題?


編輯:

我創建的測試功能,找到與AJAX調用問題。

 function testajax(){ 
      this.chrono = new Date().getMilliseconds() - this.chrono; 

      var blockingRequest = new XMLHttpRequest(); 
      blockingRequest.open("GET", "visitor_log/ajax_store_visit_duration.php?visit_duration=" + 123, false); // async = false 
      blockingRequest.send(); 

      return null; 
     } 

     window.onload = testajax; 
    </script> 
</body> 

這不是工作壓力太大。

+0

什麼/哪裏是'handlerFunction'? –

+0

沒有這樣的功能......我從一些頁面拿走了這段代碼......我可以刪除那個handlerFunction嗎? – Kamil

+0

您必須使AJAX調用同步,否則瀏覽器會在卸載時中止/停止請求。 – Ian

回答

0

好了,所以故意不使用jQuery:

這裏的PHP:

<?php 

function loggit($text) { 
    $myFile = "/tmp/test.txt"; 
    $fh = fopen($myFile, 'wb'); 
    fwrite($fh, $text); 
    fclose($fh); 
} 

if(isset($_GET["visit_duration"])) { 
    $text = $_GET["visit_duration"]; 
    loggit($text); 
} 
else die("error"); 

?> 

這裏的HTML:您要使用的異步請求(見假SENT

<!DOCTYPE html> 
<html> 
<body> 
<script language="JavaScript" type="text/javascript"> 

    function enter() { 
     this.chrono = new Date().getMilliseconds(); 
    } 

    function leave() { 
     this.chrono = new Date().getMilliseconds() - this.chrono; 

     alert("test" + this.chrono); 

    var blockingRequest = new XMLHttpRequest(); 
    blockingRequest.open("GET", "http://localhost/_TempFiles/temp.php?visit_duration=" + this.chrono.toString(), false); // async = false 
    blockingRequest.send(); 

     return null; 
    } 

    window.onload = enter; 
    window.onbeforeunload = leave; 
</script> 
</body> 
</html> 

阻止request.open) - 但要小心這是一個BLOCKING請求(因此名稱)。

此外,我改變了從「登錄」到「loggit」日誌是PHP的自然對數函數的PHP函數的名稱...

+0

這不工作。我爲此創建了測試功能,請參閱問題更新。 – Kamil

+0

奇怪的是,它適合我。我正在編輯原始答覆和兩個文件的完整源代碼,因爲它們很短。請注意,您需要將html://localhost/_TempFiles/temp.php網址替換爲html文件中的網址。 –

+0

我把'log'函數改成了'logtxt',它工作正常。這很奇怪,因爲它正在工作,當我試圖使用該功能更早... – Kamil