Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

javascript - 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.

Here is the code to disable the back button in browser, but it's working in onload event only. when i am trying to use this in onclick event it wont work.

<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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It won't work because on click, it will run before navigation--so it will go to the nonexistant "forward" page.

Use 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...