javascript 在第二次尝试刷新时如何将页面重定向到另一个页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8622892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:01:15  来源:igfitidea点击:

How to redirect a page to another page when refresh at second attempt

javascriptjquerybrowser-refresh

提问by sss

<script language="JavaScript">
  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
   alert("");window.location="index.html";
  }
</script>

Here i tried but not working please guide me

在这里我试过但没有工作请指导我

回答by Lokesh Kumar

In bbb.jsp:

bbb.jsp

window.onbeforeunload = function() { 
    window.setTimeout(function () { 
        window.location = 'AAA.jsp';
    }, 0); 
    window.onbeforeunload = null; // necessary to prevent infinite loop, that kills your browser 
}

回答by sachin babu

Windows onload loads after all the content is loaded, little bit slow other workaround is to use document.onload(Browser compatibility issue)

加载所有内容后加载Windows onload,有点慢其他解决方法是使用document.onload(浏览器兼容性问题)

window.onload = function () {
  window.location = "/allotment";
}

回答by Robin Michael Poothurai

below code will work for you

下面的代码对你有用

function confirmExit()
{
 alert("exiting");
 window.location.href='index.html';
 return true;
}
window.onbeforeunload = confirmExit;

回答by JLev

This has worked for me. Not the second attempt part, but changing URL after showing the popup. If you hit cancel, the function is not ran. If you click "Reload", the function is called and you will be redirected.

这对我有用。不是第二次尝试部分,而是在显示弹出窗口后更改 URL。如果您点击取消,则不会运行该功能。如果您单击“重新加载”,则会调用该函数并且您将被重定向。

$(window).on('beforeunload', function() {
  $(window).on('unload', function() {
    window.location.href = 'index.html';
  });

  return 'Not an empty string';
});