2

このコードは、マウスの移動座標を配列に格納し、アンロード前にポストする必要があります。しかし、それは投稿しません。私が変われば

name: name: "blabla"
に移動

できます。問題が「移動」変数にあることを意味します。どうすれば動作させることができますか?

$(document).ready(function(){


var moves = [];

$("html").mousemove(function(e){
moves.push(e.pageX + "x" + e.pageY) 
});


window.onbeforeunload = function() {

$.ajax({

      type: "POST",
      url: "mailyaz.php",
      data: {
      name: moves;
      }
      });

});

});
4

2 に答える 2

2

これを試すことができます。これは私が数ヶ月前に開発した小さな例です。この場合、座標はテキスト ファイルに保存されますが、これをデータベースへの INSERT に置き換えることができます。

クライアント側でこれを置きます:

    var moves = ""; //Now an String to store the Coords

    $(document).ready(function(){
        //When you moves the mouse inside the Page then 
        //concat the Coords into the String var and add a Line-Brak at the end
        $("html").mousemove(function(e){
            moves += (e.pageX + " x " + e.pageY + "\n");

        });

        //Here the magic happen: bind a function to onbeforeunload event that POST
        //the String to the server
        $(window).bind('beforeunload', function() {

            $.post("server.php",{name:moves});

        }); 

    });

ここで、サーバー側に server.php という名前のページが必要です。

    //Capture the String
    $cursorMoves = ($_POST['name']);

    $myFile = "testFile.txt";
    $fh = fopen($myFile, 'w');
    fwrite($fh, $cursorMoves);
    fclose($fh);
于 2012-05-19T15:33:24.407 に答える
1

onbeforeunload文字列を返す必要があります。ただし、表示されるダイアログによって ajax リクエストがブロックされます。ユーザーがページを受け入れて離れると、リクエストが中断される可能性があります。

https://developer.mozilla.org/en/DOM/window.onbeforeunload

http://jsfiddle.net/sVU7K/1

于 2012-05-19T14:24:57.377 に答える