重定向到另一个页面

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

如果我的内容帮助到了您,欢迎点个 Star 🎉🎉🎉 鼓励鼓励 :) ~~

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


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

重定向回主页:

window.location = window.location.host

更多资料

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容