【问题标题】:Issue in clicking links with window.onbeforeunload单击带有 window.onbeforeunload 的链接时出现问题
【发布时间】:2018-05-16 08:26:24
【问题描述】:

我有一个父 div content_div,其中包含表格内的表单元素,用户可以在其中选择多个行元素(可点击的行),download link elements 等。

我有一个警告弹出用户更改完成,然后尝试刷新而不点击保存按钮。

当我选择一行并尝试单击该行内的下载链接时,我不需要显示警告弹出窗口。我有以下代码,但它不起作用。

如果我没有选择任何行并尝试点击任何下载链接,它没有显示任何弹出窗口,这意味着没关系。

如果我选择任何行然后尝试单击任何下载链接,它会显示我的情况下错误的弹出窗口。

如果用户点击我的#content_div 之外的一些其他链接,它会显示弹出窗口,这对我来说是正确的。

$(function() {
  var formmodified = 0;

  //click event for each row inside the table
  $(".course_row").click(function(e) {
    -- -- -
    formmodified = 1; //setting the variable to 1 means user 
    has changed something inside the page
  });
  //when form submits 
  $("#submit").click(function() {
    formmodified = 0; //assuming that form is saved after form change
  });
  //function for warning popup
  window.onbeforeunload = confirmExit;

  function confirmExit() {
    var element_clicked = 0;
    //#content_div is the main parent which contain the entire
    contents.

    $('#content_div').children().on('click', function(e) {
      console.log('clicked');
      element_clicked = 1; //means the clicked element is inside the #content_div which can be a link or other things

    });
    console.log(element_clicked);
    EDIT: I am always getting element_clicked value as 0 when I click any element inside the div.The value 'clicked'
    is showing.I dont know why the value
    for the variable is not setting to 1


    if (element_clicked) {
      //element_clicked = false;  
      return; // abort beforeunload
    } else {
      if ((!element_clicked) && (formmodified == 1)) {
        return "The selected courses are not saved.
        Do you wish to leave the page ? ";
      }

    }

  }
});

在这种情况下请帮助我。在此先感谢

【问题讨论】:

    标签: javascript jquery hyperlink popup onbeforeunload


    【解决方案1】:

    只需将click() 事件添加到锚元素并取消绑定onbeforeunload 事件。

    $("a").on('click', function() {
      window.onbeforeunload = null;
    });
    

    这只是一个例子。您可以向选择器添加一个类或 id,以便仅在单击特定锚标记时才解除绑定事件。

    例如

    $("#content_div a").on('click', function() {
      window.onbeforeunload = null;
    });
    

    编辑

    如果您想添加onbeforeunload 进行刷新,您必须在触发任何更改时执行此操作,或者在按下F5 按钮时添加事件。

    说实话,我认为第一个解决方案更好,因为即使用户点击浏览器中的刷新按钮,弹出窗口也会出现。

    所以你必须改变你的功能。

    如果你不想。这是一个快速添加到F5 的按钮。

    $(document.body).on("keydown", this, function (event) {
        if (event.keyCode == 116) {
            window.onbeforeunload = true;
        }
    });
    

    【讨论】:

    • 当我使用这段代码时,刷新页面时弹出不来。功能不起作用
    • @Techy 这只是一个例子。您可以向选择器添加类或 id,以便仅在单击特定锚标记时才解除绑定事件。 jQuery 选择器与 css 相同
    • $(document).on("click", function (event) { if($(event.target).closest("#content_div").length == 1) { window.onbeforeunload = null; } else { window.onbeforeunload = confirmExit; } });它现在可以工作了,但是我面临的一个问题是当我尝试单击 f5 时,它正在刷新页面而没有弹出窗口
    猜你喜欢
    • 1970-01-01
    • 2015-04-07
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    相关资源
    最近更新 更多