当前位置:首页 > JavaScript

js实现页面跳转

2026-01-12 12:51:18JavaScript

使用 window.location.href

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

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

这种方式会保留浏览历史,用户可以通过后退按钮返回上一页。

使用 window.location.replace

通过 window.location.replace 方法实现跳转,但不会保留历史记录:

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

适合用于不希望用户返回的场景,如登录后跳转。

使用 window.open

在新窗口或标签页中打开页面:

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

第二个参数 _blank 表示在新标签页打开,可替换为 _self 在当前窗口打开。

使用 meta 标签自动跳转

在 HTML 中插入 meta 标签实现自动跳转:

document.head.innerHTML += '<meta http-equiv="refresh" content="3;url=https://example.com">';

content 中的数字表示延迟跳转的秒数。

使用表单提交跳转

通过动态创建表单实现跳转:

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

适用于需要提交数据的 POST 请求跳转。

使用 history.pushState

通过 History API 修改 URL 而不刷新页面:

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

适用于单页应用(SPA)的路由跳转,需配合路由库使用。

使用 location.assign

调用 location.assign 方法实现跳转:

js实现页面跳转

location.assign('https://example.com');

效果与直接修改 href 类似,但可读性更好。

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

相关文章

vue怎么实现页面返回

vue怎么实现页面返回

Vue 实现页面返回的方法 在 Vue 中实现页面返回功能,可以通过以下几种方式完成,具体取决于项目使用的路由模式和技术栈。 使用 Vue Router 的编程式导航 通过 this.$router…

vue页面分离的实现

vue页面分离的实现

Vue 页面分离的实现方法 组件化开发 Vue 的核心思想之一是组件化,通过将页面拆分为多个可复用的组件实现分离。每个组件包含独立的模板、逻辑和样式,通过 props 和 events 进行通信。…

vue实现页面分页

vue实现页面分页

Vue实现页面分页的方法 使用Element UI的分页组件 Element UI提供了现成的分页组件el-pagination,适合快速集成。以下是一个示例代码: <template>…

vue实现页面手写

vue实现页面手写

Vue 实现手写功能 在 Vue 中实现手写功能通常需要使用 HTML5 的 Canvas 元素,结合鼠标或触摸事件来捕捉用户的绘制动作。以下是实现步骤和代码示例: 基础实现步骤 创建一个 Vue…

vue实现导航跳转

vue实现导航跳转

vue-router 基本跳转方法 在 Vue 项目中实现导航跳转主要通过 vue-router 完成。安装路由依赖: npm install vue-router 在 router/index.j…

vue实现跳转高亮

vue实现跳转高亮

Vue实现路由跳转高亮 在Vue项目中实现导航菜单跳转高亮效果,通常结合vue-router的active-class特性。以下是几种常见实现方式: 使用router-link的active-cla…