当前位置:首页 > JavaScript

用js实现页面跳转

2026-03-01 15:56:44JavaScript

使用 window.location.href 进行跳转

通过修改 window.location.href 属性实现页面跳转,这是最常用的方法。

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

使用 window.location.replace 替换当前页面

replace 方法会替换当前页面,不会在浏览历史中留下记录。

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

使用 window.open 在新窗口打开

window.open 可以在新标签页或窗口中打开页面,并可控制窗口属性。

用js实现页面跳转

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

使用 location.assign 加载新页面

assign 方法会加载新页面,并在浏览历史中保留原页面记录。

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

使用 meta 标签自动跳转

在 HTML 的 <head> 中添加 <meta> 标签,设置自动跳转。

用js实现页面跳转

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

使用表单提交跳转

通过动态创建表单并提交实现跳转。

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

使用 history.pushStatereplaceState(单页应用)

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

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

使用 a 标签模拟点击

动态创建 <a> 标签并触发点击事件实现跳转。

const link = document.createElement("a");
link.href = "https://example.com";
link.click();

注意事项

  • replaceassign 的区别在于是否保留历史记录。
  • window.open 可能被浏览器弹窗拦截器阻止。
  • 单页应用跳转需配合前端路由库(如 React Router、Vue Router)。

标签: 跳转页面
分享给朋友:

相关文章

如何react页面

如何react页面

创建 React 页面 使用 create-react-app 快速初始化项目: npx create-react-app my-app cd my-app npm start 编写组件代码 在 s…

vue实现界面跳转

vue实现界面跳转

路由配置 在Vue项目中实现界面跳转通常依赖Vue Router。需在router/index.js中配置路由路径和组件映射关系: import { createRouter, createWebH…

vue单页面实现登录

vue单页面实现登录

实现登录功能的基本步骤 在Vue单页面应用(SPA)中实现登录功能,通常需要结合前端和后端交互。以下是关键步骤和代码示例: 创建登录组件 开发一个独立的登录组件,包含表单元素如用户名和密码输入框,以…

vue实现页面截屏

vue实现页面截屏

使用html2canvas库实现截屏 安装html2canvas库 npm install html2canvas --save 在Vue组件中引入并使用 import html2canvas f…

js实现页面跳转

js实现页面跳转

使用 window.location.href 通过修改 window.location.href 属性实现页面跳转: window.location.href = 'https://example…

jquery页面

jquery页面

jQuery 页面操作指南 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互的操作。以下是 jQuery 在页面中的常见使用方法…