如何屏蔽掉浏览器后退按钮的几个解决办法

Q311 How do I disable the "Back" button of a browser?

You are here: irt.org | FAQ | JavaScript | History | Q311 [ previous next ]

Easy answer - you can‘t.

Longer answer - you cannot totally remove the ability for the user to go back to the previous location, although you can make it harder.

1) You can use the location replace method when changing the location. This replaces the current history location with the new location. However older browsers do not support this method, which is why it is required to test for the images object:

<script language="JavaScript"><!--
if (document.images)
    location.replace(‘http://www.somewhere.com/apage.html‘);
else
    location.href = ‘apage.html‘;
//--></script>

Note: in Opera replace() will work, but only with absolute URL‘s.

2) You can load the new location into another window, and then close the old window:

In the first page:

<script langauge="JavaScript"><!--
function newWindow(fileName,windowName) { msgWindow=window.open(fileName,windowName); }
//--></script>

<a href="javascript:newWindow(‘1.html‘,‘window1‘)">Open Window</a>

In the next window:

<script language="JavaScript"><!--
function newWindow(fileName,windowName) { msgWindow=window.open(fileName,windowName); }
function replaceURL(fileName) { newWindow(fileName,‘window2‘); self.close(); }
//--></script>

<a href="javascript:replaceURL(‘2.html‘)">change location</a>

3) You can open a window without a toolbar:

<script language="JavaScript"><!--
msgWindow=window.open(‘apage.html‘,‘windowName‘,‘toolbar=no‘);
//--></script>

However, this does not totally stop the user from being able to go back to the previous page.

4) You can add code to the previous page to force the browser to go forwards again:

<script language="JavaScript"><!--
javascript:window.history.forward(1);
//--></script>

The following was submitted by Daniel Amsler

There is no direct way to catch the back button event, but there is a work around by using the onUnload function in combination with catching all other user events with JavaScript (see example below)

========================================================================================
CATCHING THE EVENT OF A USER PRESSING THE BACK BUTTON ON A HTML PAGE BY USING JAVASCRIPT
========================================================================================

<html>
<head>
	<meta ....>
</head>
<body bgcolor="#FFFFFF" ONLOAD="document.form2.list01.focus()" onUnload="goBack()">

<SCRIPT language="JavaScript">

    	var backButtonPressed = 1;
    	<!-- 0=no / 1=yes (assume per default that the back button will be pressed -->
    	<!--               and come into action if this was NOT the case)          -->

	function sortList() {
		backButtonPressed=0;
		document.form2.hiddenFieldName.name=‘sortButton‘;
		<!-- activate sortButton to pass its activation status to the servlet -->
		<!-- (sorting will be done by the servlet) -->
		document.form2.submit();
	}

	function printSelectedEntry() {
		backButtonPressed=0;
		document.form2.hiddenFieldName.name=‘printButton‘;
		<!-- activate printButton to pass its activation status to the servlet -->
		<!-- (printing will be done by the servlet) -->
		document.form2.submit();
	}

	function cancel() {
		backButtonPressed=0;
	 	document.form2.hiddenFieldName.name=‘cancelButton‘;
	 	<!-- activate cancelButton to pass its activation status to the servlet -->
	 	<!-- (the cancel button will be handled by the servlet) -->
		document.form2.submit();
	}

	function help() {
		backButtonPressed=0;
		document.form2.hiddenFieldName.name=‘helpButton‘;
		<!-- activate helpButton to pass its activation status to the servlet -->
		<!-- (the help button will be handled by the servlet) -->
		document.form2.submit();
	}

	function goBack() {
		if (backButtonPressed == 1) {
			backButtonWasPressed();
		}
	}

	function backButtonWasPressed() {
		<!-- do whatever you want, e.g. call your implementation of the cancel button -->
		<!-- (which is not necessarily going back to the previous page) -->
		cancel();
	}

</SCRIPT>

<table border="1" cellpadding="2" cellspacing="0">
    <tr>
    	--> display list01 (list with several entries)
    </tr>
</table>

<form action="{SYS_URL}" method="POST" name="form2">

<input type="button" name="sortButton"   value="Sort List"  onClick="sortList()">
<input type="button" name="printButton"  value="Print" 	    onClick="printSelectedEntry()">
<input type="button" name="cancelButton" value="Cancel"     onClick="cancel()">

<!-- hidden field‘s NAME parameter will be set to the function selected by the user before submitting the page -->
<input type="hidden" NAME="hiddenFieldName" VALUE="">

</form>
</body>
</html>

如何屏蔽掉浏览器后退按钮的几个解决办法

时间: 2024-11-10 13:56:23

如何屏蔽掉浏览器后退按钮的几个解决办法的相关文章

防止页面后退(使浏览器后退按钮失效)

防止页面后退(使浏览器后退按钮失效) 原理:用新页面的URL替换当前的历史纪录,这样浏览历史记录中就只有一个页面,后退按钮永远失效. 注:history.go和history.back(包括用户按浏览器历史前进后退按钮)触发, 页面由于使用pushState修改了history),会触发popstate事件.     [代码如下]       注:直接放在不想后退跳转的页面即可! 方法一:       <script type="text/javascript">     

禁用浏览器后退按钮

基本上是3个solution: 1).设置网页过期(服务器端) 2).javascript:window.history(客户端) 3).对于键盘的backspace.通过window.event来过滤,当然要考虑的是对于Input控件,要保持删除的功能. <script type="text/javascript"> function backspace() { if (event.keyCode == 8 && event.srcElement.tagN

L--怎样让用户点击浏览器后退按钮刷新后退页面的验证码

介绍 项目需要,怎样让用户点击浏览器后退按钮刷新后退页面的验证码,通过cookie来解决 方法一(通过设置前台html)(失败) 本想通过控制html的http-equiv属性来解决问题,如下 http-equiv属性 1.<meta http-equiv="Content-Type" contect="text/html";charset=gb_2312-80"> 和 <meta http-equiv="Content-Lan

javascript怎么禁用浏览器后退按钮

1. 复制代码 代码如下: <script language="JavaScript"> javascript:window.history.forward(1); </script> 利用JS产生一个“前进”的动作,以抵消后退功能,这种方法应该是最简洁的,并且不需要考虑用户连点两次或多次“后退”的情况,缺点是当用户端禁用了JavaScript之后即失效. 2. 复制代码 代码如下: <A HREF="logout.do" onclic

IE浏览器JS提示缺少对象的解决办法

前端文件中写了一个AJAX取城市列表的JS方法.一个检测表单输入的JS方法,还有一些其它的JS代码. 整个页面代码在chrome和火狐浏览器下一切正常.但是在IE浏览器下在取城市列表的JS方法中提示缺少对象,这个不太具体的报错一开始就让我头疼,因为在chrome等浏览器中是可以运行的,所以初步估计是代码冲突,于是一段代码,一段代码的删,搞了一个小时,发现问题出在检查表单的JS方法里: 表单中有一个选择产品分类的下拉框,这个下拉框的name和ID都定义为:class ,然后在检查表单的JS方法里取

ie浏览器float right 向下错位解决办法

ie  360 浏览器float right 向下错位解决办法 span 当设置其float:right;时,其向下一行错位时 最佳办法:将span移到所要显示的文字前面 其次办法:在span母容器的css里添加position:relative; 在span的css里添加position:absolute;right:0px;解决. 我选择第一种,搞定! 转载http://yijianfengvip.blog.163.com/blog/static/1752734322012324442422

根据浏览器history模拟浏览器后退按钮显隐问题

场景:在APP页面开发中,有一个需求,做一个返回按钮,实现的功能和浏览器的后退按钮相同. 措施:具体思路如下: 1.开始打开页面时,浏览器的history.length为1,按钮隐藏:     2.当history.length>1时,点击按钮执行 history.go(-1): 3.后退到历史记录栈中第一帧时,按钮隐藏.出于浏览器安全性考虑,history中没有给出属性判断其当前页的位置,所以,在开始打开页面,处于第一帧时,在当前url中添加参数,按钮点击事件触发时,根据该参数可判断是否是第一

window.open() 某些情况会被浏览器阻止弹出窗口及解决办法

window.open() 的作用是创建一个新的浏览器窗口用来打开相关的资源,这是一个原生的 Javascript API 接口. 有关 window.open() 的基本使用可以参考 mozilla 提供的  API 文档:window.open . 大部分现代的浏览器(泛指 Chrome / Firefox / IE 10+ / Safari)都默认开启了阻止弹出窗口的策略,原因是 window.open 被广告商滥用,严重影响用户的使用.这个阻止弹出窗口的操作,并不是直接封杀 windw.

IE浏览器常见CSS兼容性问题及解决办法

对于前端开发者来讲,世界上存在着一个神奇的东西——IE浏览器,尤其是低版本的IE浏览器,惨不忍睹的兼容性使其臭名昭著.前端工作者很多的时间都花在了和它打交道上,所以大家纷纷吐槽IE浏览器如何的渣,简直是万恶之源.但是IE浏览器市场份额有非常大,喷完还要接着搞兼容.对于IE浏览器来讲,我们应该有一个客观的评价.首先其兼容性差,原因可想而知,更新速度太慢,要几年才出一个版本,而市场上的FireFox.chrome等浏览器几个月就有一个版本上市.中间那么长的时间足以将其bug充分的暴露出来,其他浏览器