我可以禁用“后退”按钮浏览器功能吗?

时间:2023-01-21 14:33:43

I want to change the function of the "back" browser button. I tried with this:

我想更改“后退”浏览器按钮的功能。我试过这个:

window.onbeforeunload = function() {
    show_page(last_page, 'not-search'); 
}

and it works, in part: it calls the function but the browser navigates to the previous page anyway. Is there a solution for that?

它的工作原理部分是:它调用函数,但浏览器无论如何都会导航到上一页。有解决方案吗?

EDIT I need to do that:

index(ajax content) ---user click--->other ajax content(i call it A content)
A content --->back button ---->previous index content

编辑我需要这样做:index(ajax内容)---用户点击--->其他ajax内容(我称之为A内容)一个内容--->后退按钮---->以前的索引内容

3 个解决方案

#1


1  

Return a string in your function to warn the user and give the option to cancel.

在函数中返回一个字符串以警告用户并提供取消选项。

window.onbeforeunload = function() {
    show_page(last_page, 'not-search'); 
    return "please do not use the back button";
}

Completely disabling is both tricky and bad practise. Limiting the user in such a way could easily be seen as spam or unwanted behaviour.

完全禁用既棘手又不好。以这种方式限制用户很容易被视为垃圾邮件或不需要的行为。

edit

编辑

After I have read your edit, I would suggest to use history.pushState whenever you are modifying the page with ajax. That way the backbutton will navigate to an earlier state which is what you want. Take a look at the history API for all functions you need.

在我阅读了您的编辑后,我建议您在使用ajax修改页面时使用history.pushState。这样,后退按钮将导航到您想要的早期状态。查看所需功能的历史API。

When the previous button is pressed it will trigger window.onpopstate function where you can handle to load the correct content. See example below:

按下上一个按钮时,它将触发window.onpopstate函数,您可以在其中处理加载正确的内容。见下面的例子:

window.onpopstate = function(event) {
  alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
};

#2


-1  

To stop user from going to previous page

阻止用户转到上一页

add this js at bottom of page

在页面底部添加此js

<script language="text/javascript">
window.history.forward();
</script>

#3


-1  

Take a look at this: http://www.irt.org/script/311.htm

看看这个:http://www.irt.org/script/311.htm

Quote from the website above:

从上面的网站引用:

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.

更长的答案 - 你不能完全取消用户返回上一个位置的能力,尽管你可以让它变得更难。

After you add the update to your question:

在您的问题中添加更新后:

If you are manipulating the content of the page using Ajax, you can update the history of the browser manually. You should also consider adding hashes at the end of the url.

如果使用Ajax操作页面内容,则可以手动更新浏览器的历史记录。您还应该考虑在网址末尾添加哈希值。

For more information about the history API: https://developer.mozilla.org/en-US/docs/Web/API/History_API

有关历史API的更多信息:https://developer.mozilla.org/en-US/docs/Web/API/History_API

#1


1  

Return a string in your function to warn the user and give the option to cancel.

在函数中返回一个字符串以警告用户并提供取消选项。

window.onbeforeunload = function() {
    show_page(last_page, 'not-search'); 
    return "please do not use the back button";
}

Completely disabling is both tricky and bad practise. Limiting the user in such a way could easily be seen as spam or unwanted behaviour.

完全禁用既棘手又不好。以这种方式限制用户很容易被视为垃圾邮件或不需要的行为。

edit

编辑

After I have read your edit, I would suggest to use history.pushState whenever you are modifying the page with ajax. That way the backbutton will navigate to an earlier state which is what you want. Take a look at the history API for all functions you need.

在我阅读了您的编辑后,我建议您在使用ajax修改页面时使用history.pushState。这样,后退按钮将导航到您想要的早期状态。查看所需功能的历史API。

When the previous button is pressed it will trigger window.onpopstate function where you can handle to load the correct content. See example below:

按下上一个按钮时,它将触发window.onpopstate函数,您可以在其中处理加载正确的内容。见下面的例子:

window.onpopstate = function(event) {
  alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
};

#2


-1  

To stop user from going to previous page

阻止用户转到上一页

add this js at bottom of page

在页面底部添加此js

<script language="text/javascript">
window.history.forward();
</script>

#3


-1  

Take a look at this: http://www.irt.org/script/311.htm

看看这个:http://www.irt.org/script/311.htm

Quote from the website above:

从上面的网站引用:

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.

更长的答案 - 你不能完全取消用户返回上一个位置的能力,尽管你可以让它变得更难。

After you add the update to your question:

在您的问题中添加更新后:

If you are manipulating the content of the page using Ajax, you can update the history of the browser manually. You should also consider adding hashes at the end of the url.

如果使用Ajax操作页面内容,则可以手动更新浏览器的历史记录。您还应该考虑在网址末尾添加哈希值。

For more information about the history API: https://developer.mozilla.org/en-US/docs/Web/API/History_API

有关历史API的更多信息:https://developer.mozilla.org/en-US/docs/Web/API/History_API