Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Firefox关闭窗口之前运行函数_Javascript_Html_Ajax - Fatal编程技术网

Javascript 在Firefox关闭窗口之前运行函数

Javascript 在Firefox关闭窗口之前运行函数,javascript,html,ajax,Javascript,Html,Ajax,这是我在这里的第一个话题!所以,我尝试在Firefox窗口关闭之前运行ajax函数,我的代码在关闭选项卡时工作,但窗口不工作。我该怎么做 详细信息:只有Firefox在窗口关闭时不运行该功能,如果可能的话,不使用JQuery window.onload = function(){ window.addEventListener('beforeunload',function(event){ var ajax = new XMLHttpRequest();

这是我在这里的第一个话题!所以,我尝试在Firefox窗口关闭之前运行ajax函数,我的代码在关闭选项卡时工作,但窗口不工作。我该怎么做

详细信息:只有Firefox在窗口关闭时不运行该功能,如果可能的话,不使用JQuery

window.onload = function(){
    window.addEventListener('beforeunload',function(event){
        var ajax = new XMLHttpRequest();
        ajax.open("POST","index.php",true);
        ajax.send();
    }, false);
}
试一试

var some_ajax_data; 
window.onbeforeunload = function() {
  navigator.sendBeacon('/data.php', some_ajax_data);
};