同域名、同项目、仅 hash 变化,window.location.href 不跳转

两个 URL 只有 hash 后面的内容不同 ,浏览器会认为没有真正跳转 ,所以 window.location.href 不执行刷新

bash 复制代码
https://域名/cash-center/#/payMethod?params=abc
https://域名/cash-center/#/bindCard?params=12345

原因:浏览器只看 # 号前面的网址,不看 # 号后面的!

什么是 Hash?

URL 中 # 后面的部分叫 hash

为什么 window.location.href 不执行?

浏览器对 hash 变化 的处理方式:

bash 复制代码
❌ hash 变化 ≠ 页面跳转

变化类型

浏览器行为

hash 变化(#/payMethod#/bindCard

不刷新页面,只触发 hashchange 事件

pathname 变化(/payMethod/bindCard

刷新页面

你的情况

javascript 复制代码
// 当前 URL
window.location.href = 'https://xxx.com/cash-center/#/payMethod?params=abc'

// 目标 URL(只有 hash 不同)
window.location.href = 'https://xxx.com/cash-center/#/bindCard?params=12345'
//                                                    ^^^^^^^ hash 变了

浏览器看到只是 hash 变了,不会刷新页面,SPA 应用内部通过监听 hashchange 来切换路由。

解决方案

方案 1:强制刷新

javascript 复制代码
window.location.href = targetUrl
window.location.reload()  // 强制刷新

方案 2:使用 Vue Router(推荐)

php 复制代码
this.$router.push({
  path: '/bindCard',
  query: { params: '12345' }
})

方案 3:替换 pathname(不是 hash)

ini 复制代码
// 构造新 URL 时,换掉 hash 部分
const url = window.location.href.replace('#/payMethod', '#/bindCard')
window.location.href = url

一句话总结

hash 变化 ≠ 页面跳转,浏览器只把它当作同一页面的"锚点"变化,不会重新加载。SPA 应用靠监听 hashchange 来处理路由,而不是靠页面刷新。

比如这样的两个地址

bash 复制代码
https://tank-cash-center-h5.xyuat.com/cash-center/#/payMethod
https://tank-cash-center-h5.xyuat.com/cash-center/#/bindCard

在浏览器眼里,它们是同一个网址!

因为浏览器认为:

  • # 后面的东西 = 页面内部的标记
  • 不算新页面
  • 所以你用 window.location.href 改后面的内容
  • 浏览器懒得动,不跳转、不刷新

就像:

bash 复制代码
一本书 = 网址
书里的页码 = #后面的内容

你只是翻页,没换书,浏览器觉得:

不用重新加载,我不跳!

再简化到极致

# 前面一样 = 同一个页面

改 # 后面 = 只是翻页,不是跳转

所以:

window.location.href 不生效!

你现在必须用的唯一方案(支持返回)

kotlin 复制代码
this.$router.push('/bindCard')

或者

ini 复制代码
window.location.hash = '/bindCard'

解决方案

1、用 window.location.hash 跳转

ini 复制代码
// 拼接新的 hash 路由
const newHash = '#/bindCard?params=12345';

// 直接修改 hash → 自动跳转,自动保留历史记录(可返回)
window.location.hash = newHash;

2、用 window.location.href(加时间戳强制跳转),已验证过还是行不通

ini 复制代码
const baseUrl = 'https://tank-cash-center-h5.xyuat.com/cash-center/';
const targetHash = '#/bindCard?params=12345';

// 加时间戳,让浏览器认为是新链接,强制跳转
const fullUrl = baseUrl + targetHash + '&_t=' + Date.now();

window.location.href = fullUrl;

同域名、同项目、hash 路由、window.location.href 完全失效 ,加时间戳也没用,是因为 浏览器认为 hash 变化不算真正的页面跳转

注意:这个无法返回上一页,禁用

javascript 复制代码
window.location.replace(url) // ❌ 禁用这个!会清除历史记录,无法返回



window.location.hash = xxx   ✅ 可返回
window.location.href = xxx    ✅ 可返回
this.$router.push(xxx)        ✅ 可返回

🚀 最终最稳、最简洁、你直接用的代码

ini 复制代码
window.location.hash = '/bindCard?params=12345';

3、创建 标签模拟点击 (浏览器无法拦截,强制跳转),已验证这个方法也不行
[```ini
// 目标地址
const url = "https://tank-cash-center-h5.xyuat.com/cash-center/#/bindCard?params=12345";

// 创建 a 标签
const a = document.createElement("a");
a.href = url;
a.target = "_self"; // 在当前页面打开 → 可以返回上一页
document.body.appendChild(a);
a.click(); // 模拟点击
document.body.removeChild(a);

复制代码
### 方法 1:强制修改 location(最推荐,最简单)
```javascript
window.location.href = window.location.origin + window.location.pathname + '#/bindCard?params=12345'

方法 2:先清空 hash 再赋值(强制触发跳转)

ini 复制代码
window.location.hash = ''
setTimeout(() => {
window.location.hash = '/bindCard?params=12345'
}, 10)

方法 3:history.pushState + 刷新 hash(Vue 同项目专用)

javascript 复制代码
history.pushState({}, '', '/cash-center/#/bindCard?params=12345')
window.dispatchEvent(new HashChangeEvent('hashchange'))

方法 4:location.assign 跳转(支持返回)

ini 复制代码
const url = "https://tank-cash-center-h5.xyuat.com/cash-center/#/bindCard?params=12345"
window.location.assign(url)

方法 5:iframe 跳转(终极兜底,任何环境都能跳)

ini 复制代码
const url = "https://tank-cash-center-h5.xyuat.com/cash-center/#/bindCard?params=12345"

const iframe = document.createElement('iframe')
iframe.style.display = 'none'
iframe.src = url
document.body.appendChild(iframe)

setTimeout(() => {
document.body.removeChild(iframe)
}, 50)

🚀 你现在直接复制这个(100% 必跳)

javascript 复制代码
// 终极万能跳转(任何失效都能用)
window.location.hash = ''
setTimeout(() => {
window.location.hash = '/bindCard?params=12345'
}, 10)
```](https://link.juejin.cn?target= "")
相关推荐
Hello--_--World3 小时前
DOM事件流与事件委托、判断数据类型、深浅拷贝、对象遍历方式
前端·javascript
落魄江湖行3 小时前
进阶篇二 Nuxt4 渲染模式:SSR/SSG/CSR 怎么选
前端·vue.js·typescript·nuxt4
M宝可梦3 小时前
ReAct 与 LLM Agentic 范式:从推理到行动的完整技术科普
前端·react.js·前端框架
x-cmd3 小时前
[260416] 谷歌 Chrome 推出 Skills 功能!帮你保存、复用提示词
前端·chrome·ai·自动化·agent·x-cmd·skill
色空大师3 小时前
【Linux-安装nginx】
linux·运维·前端·nginx·部署
董董灿是个攻城狮3 小时前
封了几百万个账号的 Claude, 路走窄了
前端
heytoo3 小时前
同一个模型,为什么结果差10倍?差的不是模型
前端·agent
霪霖笙箫3 小时前
「JS全栈AI学习」九、Multi-Agent 系统设计:架构与编排
前端·面试·全栈
慕斯fuafua3 小时前
CSS——定位
前端·css