博士TL;
1-修改当前URL并添加/注入它(新的修改URL)作为一个新的URL条目到历史列表,使用pushState:
window.history.pushState({}, document.title, "/" + "my-new-url.html");
2-替换当前URL而不添加到历史条目,使用replaceState:
window.history.replaceState({}, document.title, "/" + "my-new-url.html");
3-根据您的业务逻辑,pushState将在以下情况下有用:
您希望支持浏览器的后退按钮
你想要创建一个新的URL,添加/插入/推送新的URL到历史条目,并使其成为当前URL
允许用户使用相同的参数收藏页面(以显示相同的内容)
通过statobj以编程方式访问数据,然后从锚解析
正如我从你的评论中理解的那样,你想要在不重定向的情况下清理你的URL。
注意,您不能更改整个URL。您可以更改域名后面的内容。这意味着您不能更改www.example.com/,但可以更改。com/之后的内容
www.example.com/old-page-name => can become => www.example.com/myNewPaage20180322.php
背景
我们可以用:
1-如果你想添加一个新的修改URL到历史条目,使用pushState()方法。
2-如果你想更新/替换当前历史条目,使用replaceState()方法。
. replacestate()的操作与. pushstate()完全相同,除了. replacestate()修改当前历史条目,而不是创建一个新条目。注意,这并不会阻止在全局浏览器历史记录中创建新条目。
.replaceState()在需要更新
状态对象或当前历史记录项的URL,以响应某些
用户操作。
Code
To do that I will use The pushState() method for this example which works similarly to the following format:
var myNewURL = "my-new-URL.php";//the new URL
window.history.pushState("object or string", "Title", "/" + myNewURL );
Feel free to replace pushState
with replaceState
based on your requirements.
You can substitute the paramter "object or string"
with {}
and "Title"
with document.title
so the final statment will become:
window.history.pushState({}, document.title, "/" + myNewURL );
Results
The previous two lines of code will make a URL such as:
https://domain.tld/some/randome/url/which/will/be/deleted/
To become:
https://domain.tld/my-new-url.php
Action
Now let's try a different approach. Say you need to keep the file's name. The file name comes after the last /
and before the query string ?
.
http://www.someDomain.com/really/long/address/keepThisLastOne.php?name=john
Will be:
http://www.someDomain.com/keepThisLastOne.php
Something like this will get it working:
//fetch new URL
//refineURL() gives you the freedom to alter the URL string based on your needs.
var myNewURL = refineURL();
//here you pass the new URL extension you want to appear after the domains '/'. Note that the previous identifiers or "query string" will be replaced.
window.history.pushState("object or string", "Title", "/" + myNewURL );
//Helper function to extract the URL between the last '/' and before '?'
//If URL is www.example.com/one/two/file.php?user=55 this function will return 'file.php'
//pseudo code: edit to match your URL settings
function refineURL()
{
//get full URL
var currURL= window.location.href; //get current address
//Get the URL between what's after '/' and befor '?'
//1- get URL after'/'
var afterDomain= currURL.substring(currURL.lastIndexOf('/') + 1);
//2- get the part before '?'
var beforeQueryString= afterDomain.split("?")[0];
return beforeQueryString;
}
UPDATE:
For one liner fans, try this out in your console/firebug and this page URL will change:
window.history.pushState("object or string", "Title", "/"+window.location.href.substring(window.location.href.lastIndexOf('/') + 1).split("?")[0]);
This page URL will change from:
http://stackoverflow.com/questions/22753052/remove-url-parameters-without-refreshing-page/22753103#22753103
To
http://stackoverflow.com/22753103#22753103
Note: as Samuel Liew indicated in the comments below, this feature has been introduced only for HTML5
.
An alternative approach would be to actually redirect your page (but you will lose the query string `?', is it still needed or the data has been processed?).
window.location.href = window.location.href.split("?")[0]; //"http://www.newurl.com";
Note 2:
Firefox seems to ignore window.history.pushState({}, document.title, '');
when the last argument is an empty string. Adding a slash ('/'
) worked as expected and removed the whole query part of the url string.
Chrome seems to be fine with an empty string.