Javascript 如何在没有查询参数的情况下重新加载页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7241851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:27:19  来源:igfitidea点击:

How do I reload the page without the query parameters?

javascripthtml

提问by user847495

Let's say I want to reload www.domain.com/abc?num=4

假设我想重新加载 www.domain.com/abc?num=4

But I want to reload www.domain.com/abcONLY (without everything after the question mark)

但我只想重新加载www.domain.com/abc(问号后没有所有内容)

回答by Igor Dymov

window.location = window.location.href.split("?")[0];

回答by user847495

There are a few ways to go about it:

有几种方法可以解决这个问题:

window.location = window.location.href.split("?")[0];

Or, alternatively:

或者,或者:

window.location = window.location.pathname;

回答by M_R_K

This is the best and easiest way,

这是最好最简单的方法

// similar to HTTP redirect
window.location.replace(location.pathname);

回答by Noyo

I typically try to avoid making unnecessary function calls whenever I can, especially when the information I need is already provided to me by the DOM. That said, here is probably objectively the best solution:

我通常会尽量避免进行不必要的函数调用,尤其是当 DOM 已经向我提供了我需要的信息时。也就是说,这可能是客观上最好的解决方案:

window.location = window.location.pathname + window.location.hash;

As pointed out in a commentby user qbert65536, there are many popular modern frameworks that use the hash to denote views and paths, which is why using window.location.pathnamealone is not enough.

正如用户qbert65536评论中指出的那样,有许多流行的现代框架使用哈希来表示视图和路径,这就是为什么单独使用是不够的。window.location.pathname

回答by Znarkus

Try this Javascript:

试试这个 Javascript:

location = location.pathname;

回答by mark.inman

I am assuming the user is pressing a button to make this refresh happen. If the button is inside a form element make sure the button type is set to "button" (example below):

我假设用户正在按下按钮来进行刷新。如果按钮位于表单元素内,请确保按钮类型设置为“按钮”(以下示例):

<button type='button' id='mybutton'>Button Name</button>

if the type is notset then it will default to type='submit' and will act as a form submit button and thus give you all the extra parameters in the url when reloaded.

如果设置类型,则默认为 type='submit' 并将充当表单提交按钮,从而在重新加载时为您提供 url 中的所有额外参数。

Then after that it is a simple javascript refresh call:

然后是一个简单的javascript刷新调用:

window.location.reload();

回答by Steely Wing

Reference

参考

location.search = '';

Or using relative URL, but this will leave the ?in the URL (Reference RFC1808)

或者使用相对 URL,但这将保留?在 URL 中(参考 RFC1808

<a href="?">
// JavaScript
location = '?';

回答by Andrew D.

var url="www.domain.com/abc?num=4";
window.location.href=url.replace(/^([^\?]+)(\??.*)$/gi,"");

回答by Matija

Plain and simple, just tested:

简单明了,刚刚测试:

window.location.href = location.pathname;

回答by Paul

top.location.href = top.location.protocol+top.location.host+top.location.pathname