0
点赞
收藏
分享

微信扫一扫

重定向到另一个页面

本文已整理到 Github,地址 👉 ​​blog​​。


我希望我的内容可以帮助你。现在我专注于前端领域,但我也将分享我在有限的时间内看到和感受到的东西。

使用 HTML:

<meta http-equiv="refresh" content="0;URL=https://github.com/lio-zero" />

使用 JavaScript:

window.location.replace('https://github.com/lio-zero')
window.location.assign('https://github.com/lio-zero')
window.location.href = 'https://github.com/lio-zero'

document.location.href = 'https://github.com/lio-zero'

window.self.location = 'https://github.com/lio-zero'
window.top.location = 'https://github.com/lio-zero'

  • 你可以省略​​window​​,因为它是全局对象
  • 使用​​location.replace​​​ 比​​location.href​​​ 更好,因为​​replace​​ 不会在会话历史记录中保留原始页面,这意味着用户不会陷入无休止的后退按钮失败。
  • 如果要模拟某人单击链接,请使用​​location.href​
  • 如果要模拟 HTTP 重定向,请使用​​location.replace​
  • ​window.self​​​ 返回一个指向当前​​window​​ 对象的引用。
  • ​window.top​​ 返回窗口层级最顶层窗口的引用。

重定向回主页:

window.location = window.location.host

举报

相关推荐

0 条评论