jQuery beforeunload事件没有在后退按钮上触发

时间:2016-01-26 16:32:59

标签: javascript jquery google-chrome backbone.js

我有一个骨干应用程序,我想在用未保存的更改导航之前提醒用户。我有一个事件要做:

$(window).bind('beforeunload', function() {
    var status = {
        unsavedEdits: false
    };

    Backbone.trigger('producerApp:navigate', status);
    if (status.unsavedEdits) {
        return status.msg || 'There are unsaved edits. Continuing will discard these edits.';
    }
});

问题是,单击后退按钮时,Chrome中不会触发此操作。我没有读过什么表明应该是这种情况。这里有什么问题吗?

2 个答案:

答案 0 :(得分:0)

编辑:



<?php
if(isset($_POST['id'], $_POST['twd'])){
    /*Importing our db connection script*/
    require_once('dbConnect.php');
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    $id = mysqli_real_escape_string($con, $_POST['id']);
    $twd = mysqli_real_escape_string($con, $_POST['twd']);

    $sql ="SELECT MAX(id) as MaxId FROM work_details WHERE twd = '$twd'";

    if ($result = mysqli_query($con, $sql)) {
        /* fetch associative array */
        if ($row = mysqli_fetch_row($result)) {
            if($row[0] === $id){
                $sql ="DELETE FROM work_details WHERE id='$id';";
                if ($result = mysqli_query($con, $sql)) {
                    echo 'success';
                }else{
                    echo 'failed';
                }
            }
            else{
            echo 'list cannot be deleted ';
        }

        }

        /* free result set */

    }
    /* close connection */
    mysqli_close($con);
}
?>
&#13;
$(document).ready(function() {
  $(window).bind('beforeunload', function() {
    var status = {
      unsavedEdits: true
    };

    if (status.unsavedEdits) {
      return 'There are unsaved edits. Continuing will discard these edits.';
    }
  });
});
&#13;
&#13;
&#13;

我认为该活动是 onbeforeunload

使用它。试穿我的机器。有效。

答案 1 :(得分:0)

只要与页面有初始交互,此代码也应在后退按钮上起作用。

window.onbeforeunload = function(){
return "are you sure you want to navigate away?";
}
  

注意:为避免不必要的弹出窗口,浏览器可能不会显示在beforeunload事件处理程序中创建的提示,除非已与该页面进行了交互,甚至可能根本不显示它们。

这是来自https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload