微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript-Ajax发布onbeforeunload不起作用

代码将鼠标移动坐标存储在数组中,并且应该在卸载之前将其发布.但它不发布.如果我改变

名称:招式

名称:“ blabla”

有用.表示问题出在“ moves”变量上.我该如何运作?

$(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;
      }
      });

});

});

解决方法:

你可以试试看
这是我几个月前开发的一个小例子.
在这种情况下,坐标存储在文本文件中,但是您可以将其替换为在数据库中插入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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐