当前位置:首页 > JavaScript

js实现跳转

2026-04-03 17:35:30JavaScript

使用 window.location.href

通过修改 window.location.href 属性实现页面跳转。

window.location.href = "https://example.com";

使用 window.location.replace

href 类似,但不会在浏览器历史记录中留下当前页面的记录。

window.location.replace("https://example.com");

使用 window.open

在新窗口或标签页中打开链接,可通过参数控制打开方式。

window.open("https://example.com", "_blank");

使用 location.assign

href 类似,显式调用跳转方法。

window.location.assign("https://example.com");

使用 meta 标签自动跳转

通过 HTML 的 <meta> 标签实现自动跳转,适合静态页面。

<meta http-equiv="refresh" content="0;url=https://example.com">

使用表单提交跳转

通过动态创建表单并提交实现跳转,适合需要传递参数的场景。

const form = document.createElement("form");
form.method = "GET";
form.action = "https://example.com";
document.body.appendChild(form);
form.submit();

使用 history.pushState 或 replaceState

适用于单页应用(SPA),仅更新 URL 而不刷新页面。

history.pushState({}, "", "/new-page");

使用导航 API(实验性)

现代浏览器支持的 Navigation API,适用于 SPA。

js实现跳转

navigation.navigate("https://example.com");

注意事项

  • 跳转前可检查 confirm() 或异步逻辑。
  • 部分方法可能受浏览器安全策略限制(如跨域)。
  • 单页应用推荐使用路由库(如 React Router、Vue Router)。

标签: 跳转js
分享给朋友:

相关文章

vue实现页面跳转

vue实现页面跳转

vue实现页面跳转的方法 在Vue中实现页面跳转主要有以下几种方式: 使用router-link组件 router-link是Vue Router提供的组件,用于声明式导航: <router…

js实现文件下载

js实现文件下载

使用 a 标签下载文件 通过动态创建 a 标签并设置 download 属性实现文件下载。适用于已知文件 URL 或 Blob 数据的情况。 function downloadFile(url, f…

php实现跳转

php实现跳转

PHP 实现页面跳转的方法 在 PHP 中实现页面跳转有多种方式,以下是常见的几种方法及其适用场景: 使用 header() 函数 通过 header() 函数发送 HTTP 头部信息实现跳转。注意…

js 实现滚动

js 实现滚动

实现滚动的方法 使用 window.scrollTo() window.scrollTo() 方法可以滚动到文档中的特定位置。可以指定 x 和 y 坐标,或者使用平滑滚动的选项。 // 滚动到指定位…

js实现图片放大

js实现图片放大

使用 CSS transform 实现图片放大 通过 CSS 的 transform: scale() 属性实现图片放大效果。当鼠标悬停时,图片会平滑放大。 const img = document…

js实现同步

js实现同步

同步与异步的概念 在JavaScript中,同步代码会按照顺序执行,每一行代码必须等待上一行代码执行完成后才能运行。异步代码则允许在等待某些操作(如网络请求)完成的同时继续执行其他代码。 实现同步的…