【问题标题】:How to reload a page without its parameters?如何在没有参数的情况下重新加载页面?
【发布时间】:2017-06-27 11:05:57
【问题描述】:

我使用location.relaod() 重新加载当前页面(是否从缓存中)。这会重新加载页面 - 在 URL 中使用所有 GET 参数。

如果当前页面是https://www.example.com/somewhere/page.html?a=3&b=4,是否可以触发https://www.example.com/somewhere/page.html的重新加载?

我计划使用的当前解决方案是

var fullURL = "https://www.example.com/somewhere/page.html?a=3&b=4"
var splitURL = fullURL.split('?')[0]
document.write(splitURL)

但也许已经实现了一种更 Javascript 的方式?

Javascriphic 借自 Python -> Pythonic

【问题讨论】:

  • 我很困惑 - 你的标题说“没有参数”,但在问题中你说 is it possible to trigger the reload of https://www.example.com/somewhere/page.html?a=3&b=4 包含所有参数。你想做什么?
  • @Xatenev:对不起,当我意识到我忘记删除第二个 URL 中的参数时,我开始澄清这个问题。固定。

标签: javascript


【解决方案1】:

使用location 对象

// similar behavior as an HTTP redirect
window.location.replace(location.protocol + '//' + location.host + location.pathname);

// similar behavior as clicking on a link
window.location.href = location.protocol + '//' + location.host + location.pathname;

【讨论】:

  • 与我计划使用的 split() 相比,这种结构有优势吗?
  • 可能不会。如果我不需要,我个人不会进行字符串操作,因为所有信息都已经存在。但是YMMV
【解决方案2】:
window.location=window.location.split("?")[0];

只需重定向..

【讨论】:

    【解决方案3】:

    打开控制台查看document.location,你可以从其中一些字段中构建你需要的url,比如host+pathname

    【讨论】:

    • 怎么来的?他已经知道他必须做些什么来更改网址,我只是告诉他在哪里可以找到已经完成了他手动使用 .split 完成的操作
    • 以上有更多的评论意图。答案如下所示……具体的工作代码示例证明可以解决他最初的问题。
    猜你喜欢
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 2012-02-23
    • 2011-11-02
    相关资源
    最近更新 更多