页面跳转方法

1. 使用 location 对象的其他方法

locationwindow 的属性,但可以直接通过 location 访问其方法和属性(无需 window. 前缀):

  • location.assign(url)
    跳转到新页面(添加历史记录,可返回)。
    location.assign("https://www.example.com");
  • location.replace(url)
    替换当前页面(不添加历史记录,不可返回)。
    location.replace("https://www.example.com");
  • location.reload()
    刷新当前页面。
    location.reload(); // 从缓存加载
    location.reload(true); // 从服务器重新加载

2. 使用 URLSearchParams 处理查询参数

URLSearchParams 是专门处理 URL 查询参数的接口,适合动态修改 URL 参数:

// 获取当前 URL 的查询参数
const url = new URL("https://example.com?name=John&age=30");
const params = new URLSearchParams(url.search);

// 添加/修改参数
params.set("age", 31);
params.append("city", "New York");

// 构造新的 URL
url.search = params.toString();
console.log(url.href); 
// 输出: https://example.com?name=John&age=31&city=New+York

3. 使用 URL 对象构造和解析 URL

URL 对象可以解析和构建完整的 URL,支持链式操作:

// 解析现有 URL
const url = new URL("https://example.com/path?query=1");

// 修改 URL 的各个部分
url.protocol = "https";
url.pathname = "/new-path";
url.searchParams.set("query", "2");

console.log(url.href); 
// 输出: https://example.com/new-path?query=2

4. 使用 fetch 或 XMLHttpRequest 发起请求

如果需要发送 HTTP 请求(如 POST),可以使用 fetchXMLHttpRequest,但不会直接跳转页面:

// 使用 fetch 发送 POST 请求
fetch("https://api.example.com/data", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ key: "value" })
})
  .then(response => response.json())
  .then(data => console.log(data));

// 使用 XMLHttpRequest
const xhr = new XMLHttpRequest();
xhr.open("GET", "https://api.example.com/data", true);
xhr.onload = () => console.log(xhr.responseText);
xhr.send();

5. 使用表单提交实现 POST 跳转

通过动态创建隐藏表单,实现 POST 请求跳转:

const form = document.createElement("form");
form.method = "POST";
form.action = "https://example.com/submit";
form.style.display = "none";

const input = document.createElement("input");
input.name = "token";
input.value = "abc123";
form.appendChild(input);

document.body.appendChild(form);
form.submit();

6. 使用前端路由库(如 Vue Router / React Router)

在单页应用(SPA)中,通常使用前端路由库管理页面跳转:

// Vue Router 示例
this.$router.push({ path: "/home" });

// React Router 示例
import { useHistory } from "react-router-dom";
const history = useHistory();
history.push("/home");

7. 使用 history API 实现无刷新跳转

history.pushState()history.replaceState() 可以修改 URL 而不刷新页面:

// 添加新的历史记录
history.pushState({ page: 1 }, "Page 1", "/page1");

// 替换当前历史记录
history.replaceState({ page: 2 }, "Page 2", "/page2");

// 监听 URL 变化
window.addEventListener("popstate", (event) => {
  console.log("当前 URL:", location.pathname);
});

8. 使用第三方库简化 URL 操作

一些轻量级库(如 urljsJSUrlify)提供了更简洁的 API:

// 使用 urljs 修改 URL
const url = new URL("https://example.com");
url.set("path", "/new-path");
url.addParam("query", "1");
console.log(url.toString()); 
// 输出: https://example.com/new-path?query=1

9. 使用 location.href 的变体

虽然 window.location.href 是最常用的方式,但也可以直接使用 location.href

location.href = "https://www.example.com"; // 等同于 window.location.href

10. 使用 location 的属性直接修改 URL

通过修改 location 的属性(如 pathnamesearch)间接跳转:

// 修改路径
location.pathname = "/new-path";

// 修改查询参数
location.search = "?query=1";

// 修改锚点
location.hash = "#section1";

总结:不同方法的适用场景

方法/技术适用场景是否刷新页面
window.location简单跳转或刷新
URLSearchParams动态修改查询参数❌(需手动更新 URL)
URL 对象构造和解析完整 URL❌(需手动更新)
fetch/XMLHttpRequest异步请求数据(不跳转)
表单提交POST 请求跳转
前端路由库单页应用(SPA)中的无刷新跳转
history API无刷新修改 URL(需手动处理逻辑)
第三方库(如 urljs)复杂 URL 操作的简化❌(需手动更新)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端与小赵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值