简体   繁体   English

如何使用Java脚本控制浏览器按钮?

[英]How to control the browser buttons using java script?

I need to disable the browser back button while cliking the submit button in html page to secure the process has not terminated. 我需要在html页面中单击“提交”按钮时禁用浏览器的“后退”按钮,以确保进程没有终止。

Here is the code to disable the back button in browser, but it's working in onload event only. 这是在浏览器中禁用后退按钮的代码,但仅在onload事件中有效。 when i am trying to use this in onclick event it wont work. 当我试图在onclick事件中使用它时,它将无法正常工作。

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>New event</title>
</head>
<SCRIPT type="text/javascript">
window.history.forward();
function noBack() { window.history.forward(); }
</SCRIPT>
</HEAD>
<BODY onload="noBack();"
onpageshow="if (event.persisted) noBack();" onunload="">
<a href='page2.html' >Page 2</a>
</body>
</html>

Can anyone know how to disable the browser back button while i am clicking submit button in html page 当我单击html页面中的提交按钮时,谁能知道如何禁用浏览器后退按钮

It won't work because on click, it will run before navigation--so it will go to the nonexistant "forward" page. 它不起作用,因为在单击时它将在导航之前运行-因此它将转到不存在的“转发”页面。

Use onbeforeunload 使用onbeforeunload

var submitFlag=false;
//set submitFlag to true on onclick of submit button
window.onbeforeunload=function(e){ 
 if(submitFlag){
  return false;
 }
 return "Are you sure you want to leave?"

 }

This cannot force the user though. 但是,这不能强迫用户。 You can never force the user not to go back. 您永远不能强迫用户不要返回。 Otherwise, this can lead to big security issues. 否则,这可能会导致严重的安全问题。

Don't do it. 不要这样 It's strongly recommended you do not break the functionality of the back button. 强烈建议您不要破坏后退按钮的功能。

See Disabling Back button on the browser 请参阅在浏览器上禁用“后退”按钮

You can't disable the browser back button. 您不能禁用浏览器后退按钮。 Not via JavaScript, or anything else. 不通过JavaScript或其他方式。 On the server side, you can keep a track of whether your submit button has been pressed and take action accordingly. 在服务器端,您可以跟踪是否已按下“提交”按钮并采取相应的措施。

You cannot disable the browser back button. 您不能禁用浏览器的后退按钮。

The code snippet you give does not "disable" the back button. 您提供的代码段不会“禁用”后退按钮。 I am not convinced you understand what it does. 我不能说服您理解它的作用。

What it does: 它能做什么:

  1. In the script tag, simulate a click on the "forward button" ( window.history.forward() ). 在脚本标签中,模拟单击“前进按钮”( window.history.forward() )。 That might take us back to where we came from if the user just pressed in the back button. 如果用户只是按下“后退”按钮,则可能会将我们带回原来的位置。

  2. When the page is loaded (so the previous hack did not work), and the page was loaded from the history cache ( event.persisted ) then simulate a click on the forward button. 当页面被加载时(因此先前的破解不起作用),并且页面是从历史记录缓存中加载的( event.persisted ),然后模拟对前进按钮的单击。

At no point there was the back button disabled. 始终没有禁用后退按钮 Instead, what you have done is write a page that breaks the back behavior for the following page. 取而代之的是,你做了什么是写一个页面,符以下页面后的行为

You might want to use the onbeforeunload event instead. 您可能想使用onbeforeunload事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM