阿贾克斯后onbeforeunload的不工作 [英] Ajax post onbeforeunload doesn't work
问题描述
这code店的鼠标移动坐标阵列,它应该张贴onbeforeunload的。但它并没有发布。如果我更改
名称:移动
到
名称:布拉布拉
它的工作原理。意思是说,问题出在动作变量。我怎样才能使它工作?
$(文件)。就绪(函数(){
VAR移动= [];
$(HTML)。鼠标移动(功能(E){
moves.push(e.pageX +×+ e.pageY)
});
window.onbeforeunload =功能(){
$阿贾克斯({
键入:POST,
网址:mailyaz.php
数据: {
名称:移动;
}
});
});
});
您可以试试这个。 这是一个小例子,我开发了一些几个月前。 在这种情况下,坐标存储在一个文本文件,但你可以用插入到数据库代替。
在客户端把这个:
VAR移动=; //现在,一个字符串存储COORDS
$(文件)。就绪(函数(){
//当你将鼠标移动到页面里,然后
// CONCAT的COORDS到字符串var和增加线路布拉克在年底
$(HTML)。鼠标移动(功能(E){
移动+ =(e.pageX +X+ e.pageY +\ N);
});
//这里魔术发生:绑定一个函数来onbeforeunload事件的POST
//字符串到服务器
$(窗口).bind('beforeunload',函数(){
$。员额(server.php,{名称:移动});
});
});
现在你需要一个页面在服务器端被称为server.php其中包含
//捕获字符串
$ cursorMoves =($ _ POST ['名称']);
$ MYFILE =TESTFILE.TXT;
$ FH =的fopen($ MYFILE,'W');
FWRITE($跳频,$ cursorMoves);
fclose函数($ FH);
This code stores mouse movement coordinates in array and it should post it onbeforeunload. But it doesn't post. If I change
name: moves
to
name: "blabla"
it works. Means that the problem is on the "moves" variable. How can I make it working ?
$(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;
}
});
});
});
You can try this. It's a little example that I develop some months ago. In this case the coordinates are stored in a Text File, but you can replace this with an INSERT into a DataBase.
On the client Side put this:
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});
});
});
Now you need a Page in the server side called server.php which contains
//Capture the String
$cursorMoves = ($_POST['name']);
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w');
fwrite($fh, $cursorMoves);
fclose($fh);
这篇关于阿贾克斯后onbeforeunload的不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!