如何禁用浏览器后退操作 [英] How to disable browser back action

查看:85
本文介绍了如何禁用浏览器后退操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的aspx页面上,我有一个标签控件。

在每个标签中,我正在加载带有URL的Iframe。这个Iframe URL存储在db表中。

每个选项卡都有自己的Iframe。

选项卡是通过读取表中的数据动态生成的。

当焦点在iframe上并且用户按下后退按钮时,它会尝试在Iframe中加载前一个URL并给出404错误。

如何处理这个退格按钮的情况?

当用户点击浏览器后退按钮时会发生同样的事情。



有没有办法处理这两种情况?



任何帮助表示感谢。



我尝试过:



我尝试在javascript中捕获keydown事件上的KeyCode,并将window.location.href设置为同一页面。

这在父页面上运行良好。但是当关注选项卡内容时,我没有任何控制权。



同样对于我的父页面,我正在通过

protected void Page_Init(object Sender,EventArgs e)

{

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));

Response.Cache.SetNoStore();

}

Hi,
On my aspx page, I have a tab control.
In each tab I am loading an Iframe with URL. This Iframe URL is stored in a db table.
Each tab has its own Iframe.
Tabs are generated dynamically by reading a data in a table.
When focus is on Iframe and user presses back button, it tries to load previous url in Iframe and gives 404 error.
How can I handle this backspace button case?
Same thing happens when user click browser back button.

Is there any way to handle these two scenarios?

Any help appreciated.

What I have tried:

I tried capturing KeyCode on keydown event in javascript and set window.location.href to the same page.
This works well on parent page. But when focus is on tab content, I dont have any control over there.

Also for my parent page, I am disabling cache on Page Init event by
protected void Page_Init(object Sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();
}

推荐答案

你不能。这完全是一个浏览器控件,你的代码无法访问它们。



但是,您可能会强制缓存过期以禁用浏览器后退按钮做完了。您可以尝试使用此JavaScript黑客来阻止浏览:禁用浏览器返回使用JavaScript的按钮功能 [ ^ ]
You can't. This is entirely a browser control and your code can't access them.

However, you could probably force the cache to expire to disable the browser back button as you've done. You could try using this JavaScript hack to prevent browsing back here: Disable Browser Back Button Functionality using JavaScript[^]


我处理键盘退格按钮的方式是在KeyDown事件上返回false。

The way I handled keyboard backspace button is to return false on KeyDown event.
document.onkeydown = function (e) {
	try {
		// IF backspace button is pressed, do not redirect the url.
		// keycode for backspace - 8 

	    if (e.keyCode === keyCode.backSpace) {
			return false;
		}
	} catch (e) {
		return true;
	}
};


这篇关于如何禁用浏览器后退操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆