防止在单击超链接时触发window.onbeforeunload事件

时间:2015-06-15 07:41:20

标签: javascript jquery model-view-controller

我有一个页面,提示用户使用window.onbeforeunload事件处理程序确认卸载页面。即使由于单击超链接(锚标记)而导致从一个页面导航到另一个页面,此消息当前也会被触发。

如何在单击超链接或锚标记时停止触发事件?我的代码如下:

<script type="text/javascript">
  $(function () {
    $("a").click(function () {
        window.onbeforeunload = null;
    });

    $("form").live("submit", submitListener);
      function submitListener(e) {
        window.onbeforeunload = null;
      }
  });
  window.onbeforeunload = confirmExit;
  function confirmExit() {
    return "Your changes will be lost if not saved...";
  }
</script>

<a title="Delete Document" class="btn btn-danger" 
   onclick="return confirm('Are you sure, you want to delete this Committee?');" 
   href='@Url.Action("Delete", "ManageCommittee", new { id =Model.Id})' 
   style="margin-left:5px;">
     <i class="icon-trash icon-white"></i>&nbsp;<span>Delete</span>
</a>


<p>I dont want alert while user click on the anchor tag</p>

0 个答案:

没有答案