在刷新时执行Javascript没有“您确定要离开页面”提示

时间:2016-03-31 12:36:24

标签: javascript jquery

在某种情况下,我需要在用户刷新页面时执行几行。

目前我使用onbeforeunload事件执行此操作,如下所示,但我正在寻找一种方法,可以在没有提示的情况下执行此操作。我只是希望它发生在幕后

解决方案可以是纯JS(首选),也可以是基于jQuery的。

//to kill the modal incase refresh or page exit
window.onbeforeunload = closingCode;
function closingCode() {

    //bunch of functions here to run before refresh/close


    window.location.href = window.location.href.split('#')[0]; //remove leftover hash symbol if it exists
    return false;
}

1 个答案:

答案 0 :(得分:1)

试试这个;)

window.onbeforeunload = function(){
  inst.close();
  inst.destroy();
  window.location.href = window.location.href.split('#')[0]; //remove leftover hash symbol if it exists
  return;
}