当前位置:首页 > 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 在当前窗口打开。

js实现页面跳转

使用 meta 标签自动跳转

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

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

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

使用表单提交跳转

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

js实现页面跳转

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 方法实现跳转:

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

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

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

相关文章

vue实现跳转

vue实现跳转

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转可以通过多种方式,包括使用 Vue Router 进行编程式导航或声明式导航,以及直接使用原生 JavaScript 方法。以下是几种常见的实现方…

h5页面实现录音

h5页面实现录音

实现H5页面录音的方法 使用Web Audio API Web Audio API提供音频处理能力,结合getUserMedia可实现录音。核心步骤包括请求麦克风权限、创建音频上下文和处理音频流。…

h5实现网页跳转

h5实现网页跳转

H5实现网页跳转的方法 在HTML5中,可以通过多种方式实现网页跳转。以下是几种常见的方法: 使用<a>标签 通过超链接标签实现跳转是最传统的方式: <a href="http…

vue实现拼接路径实现跳转

vue实现拼接路径实现跳转

在Vue中拼接路径并实现跳转 使用Vue Router的router.push方法结合模板字符串或路径拼接方法 // 在方法中拼接路径 methods: { navigateTo(pat…

vue实现页面手写

vue实现页面手写

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

vue 实现页面跳转

vue 实现页面跳转

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转可以通过多种方式,主要包括使用 Vue Router 提供的导航方法和原生 JavaScript 的方式。 使用 Vue Router 的 ro…