简体   繁体   English

如何检测后退按钮单击

[英]How to Detect Back button click

I am working on an HTML website where I have 4 divs in an index.html page. 我正在一个HTML网站上工作,我在index.html页面中有4个div。

Earlier on clicking the div section used to expand it and the other section became smaller in width. 之前单击用于展开它的div部分,另一部分的宽度变小。

Then the client asked to make sure that the URL was changed when we click any of the div. 然后客户端要求确保在我们点击任何div时更改了URL。

Suppose we click the div called test . 假设我们单击名为test的div。 The page url shoul become /test.html, without the page reloading. 页面url应该成为/test.html,没有页面重新加载。 I have implemented this functionality using the history API. 我已使用history API实现了此功能。

Now when I click on the test div and the url becomes /test.html and then if I click a link, it redirects to another page. 现在当我点击test div并且url变成/test.html然后如果我点击一个链接,它会重定向到另一个页面。

From this page, if I click on the browser back button, then it takes me to /test.html which does not exist and we get a 404 error. 在这个页面中,如果我点击浏览器后退按钮,那么它将我带到/test.html,它不存在,我们收到404错误。

I tried making a redirection page called test.html which redirects back to the index.html page, but we get a blink while doing so. 我尝试创建一个名为test.html的重定向页面,该页面重定向回index.html页面,但是这样做会让我们眨眼。

What I want to ask is whether there is a solution for this problem? 我想问的是这个问题是否有解决方案?

If you have mod_rewrite enabled on the web server, you could set this up as a URL rewrite rule. 如果在Web服务器上启用了mod_rewrite ,则可以将其设置为URL重写规则。

RewriteEngine On
RewriteRule ^test.html originalfile.html [NC]

This will redirect test.html to the original html file when the user presses the back button and navigates to test.html while the browser address bar still displays test.html . 当用户按下后退按钮并导航到test.html时,这会将test.html重定向到原始的html文件,而浏览器地址栏仍显示test.html From here, you should be able to detect the address in javascript and display the appropriate div . 从这里,您应该能够在javascript中检测地址并显示相应的div

(Note that you will probably need to modify the above rewrite rules depending on how your server is configured. For more information about creating rewrite rules, see this link . (请注意,您可能需要根据服务器的配置方式修改上述重写规则。有关创建重写规则的更多信息, 请参阅此链接

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

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