在浏览器关闭时调用ajax,而不是URL更改

时间:2015-09-25 10:16:12

标签: php jquery ajax

我想在用户不从应用程序手动注销时插入注销时间戳,即,如果用户在没有从系统注销的情况下关闭浏览器,系统应该使用jquery注销浏览器关闭事件或任何其他解决方案来执行相同操作,ajax和php受到欢迎。我尝试了以下代码:

$(document).ready(function() {
    $("#type :checkbox").click(function() {
        $("td").parent().hide();
        $("#type :checkbox:checked").each(function() {
            $("." + $(this).val()).parent().show();
        });
    });
    $("#fee :checkbox").click(function() {
        $("td").parent().hide();
        $("#fee :checkbox:checked").each(function() {
            $("." + $(this).val()).parent().show();
        });
    });
    $(document.repaymentcalc.homevalue).onchange = repayment;
    $(document.repaymentcalc.loanamount).onchange = repayment;
    $(document.repaymentcalc.interestrate).onchange = repayment;
    $(document.repaymentcalc.numberpayments).onchange = repayment;
    // populate repayment in table
    $('tbody tr').each(function(idx, row) {
        var $row = $(row);
        var initialRateCell = $row.find('td')[2]; // get column 4 (for the inital rate)
        var repaymentCell = $row.find('td').last() // get the output cell (last in the row)
        var rate = parseFloat($(initialRateCell).html()); // parse the rate from the rate cell
        var repaymentVal = computeRepayment(rate); // compute the new repayment value
        console.log(repaymentVal)
        $(repaymentCell).html(repaymentVal.repayment); // stick it in the result cell
    });
});

function computeRepayment(rate) {
    // if the passed in rate is available, use it. 
    // otherwise pull it from document.repaymentcalc
    var rate = rate || document.repaymentcalc.interestrate.value
    var x = parseInt(document.repaymentcalc.loanamount.value, 10);
    var y = parseInt(rate * 100, 10) / 120000;
    var z = parseInt(document.repaymentcalc.numberpayments.value, 10) * 12;
    var h = parseInt(document.repaymentcalc.homevalue.value, 10);

    var repayment = y * x * Math.pow((1 + y), z) / (Math.pow((1 + y), z) - 1);

    var loantovalue = x / h * 100;

    var year = z / 12;
    return {
        repayment: repayment,
        loantovalue: loantovalue,
        year: year
    }
}

function repayment(rate){
    var repaymentInfo = computeRepayment(rate);
    document.getElementById("repayments").innerHTML = 'Monthly Repayment: £' + repaymentInfo.repayment.toFixed(2);
    document.getElementById("ltv").innerHTML = 'Loan to Value: ' + repaymentInfo.loantovalue.toFixed(1) + '%';
    document.getElementById("years").innerHTML = repaymentInfo.year + ' years';
}

但是window.onbeforeunload函数()每次在webkit浏览器中更改URL时都会将我注销,就像它的特性一样。我需要一些跨浏览器解决方案。我在stackoverflow上看到了关于这个问题的各种问题,但是在我的案例中我找不到任何帮助我的解决方案。

我还需要提一下,在logout.php中我使用代码终止会话并在数据库中插入查询以及注销时间戳。

1 个答案:

答案 0 :(得分:0)

我想建议在浏览器关闭时使用以下代码进行ajax调用。

<script type="text/javascript">
    window.onbeforeunload = function() {

        var ticketId = $('#ticketId').val();
        $.ajax({
            url: '/ticket-window-close/'+ticketId,
            type: 'GET',
            async: false,
            timeout: 10000
        });
    };
</script>