简体   繁体   English

如何在不影响浏览器历史记录的情况下实现导航?

[英]How do I achieve navigation without affecting browser history?

If you go here: http://twitter.com/account/resend_password如果你在这里 go: http://twitter.com/account/resend_password

Then click on "Send Instructions" without filling in the form, it appears to be doing a full, non-ajax request.然后在不填写表格的情况下单击“发送说明”,它似乎正在执行一个完整的非 ajax 请求。

However, if I look at my browser history, the previous page (resend_password) isn't in the history.但是,如果我查看浏览器历史记录,前一页 (resend_password) 不在历史记录中。 It's as if it made an ajax request.就好像它发出了 ajax 请求。

What is twitter doing? twitter 在做什么? Is this really just an ajax request, or is it doing something tricky?这真的只是一个 ajax 请求,还是在做一些棘手的事情?

window.location.replace will load a page but won't save it in the history. window.location.replace将加载一个页面,但不会将其保存在历史记录中。

window.location.replace('http://twitter.com/account/resend_password');

This is not an ajax request.这不是 ajax 请求。

It is not something tricky either.这也不是什么棘手的事情。

Twitter is only answering the HTTP POST request with an HTTP response code number 302 (Moved Temporarily) - see http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection . Twitter is only answering the HTTP POST request with an HTTP response code number 302 (Moved Temporarily) - see http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection . The browser answers as if it was an HTTP response code number 303 (See Other).浏览器回答好像它是一个 HTTP 响应代码号 303(请参阅其他)。

In fact, what happens is merely a redirection to the same page .事实上,所发生的只是重定向到同一页面


Edit : added code for testing purposes.编辑:添加代码用于测试目的。

I have just tested with the following code, and it confirms what is said above:我刚刚用下面的代码进行了测试,它证实了上面所说的:

<form method="post"><input type="submit" value="click here" /></form>
<?php
if ( $_SERVER['REQUEST_METHOD'] == 'POST' )
    header( 'Location: /test.php', true, 302 );
?>

I am using PHP as the server-side language.我使用 PHP 作为服务器端语言。 I saved this script as test.php in the webserver public root folder.我将此脚本作为test.php保存在网络服务器公共根文件夹中。 It behaves exactly as the Twitter page: clicking the form submit button, the 302 redirection is triggered by using the header function.它的行为与 Twitter 页面完全相同:单击表单提交按钮,使用header function 触发302 重定向 The browser history does not change.浏览器历史不会改变。

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

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