同域名、同项目、仅 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= "")
相关推荐
福兮说2 小时前
设计稿是 #4A7C6F,页面量出来是 #4B7C6F:HEX、HSL、透明度、canvas 来回转的七个坑
前端·javascript·css·canvas
郑州光合科技余经理3 小时前
海外版外卖加盟:总站与分站配送规则怎么分开管
java·开发语言·前端·后端·uni-app·php·ai编程
小呆呆6664 小时前
副业搞起来,小说,漫画,漫剧的成本优化思路
前端·后端·面试
凤城老人4 小时前
从 PyQt6 到 Electron:给 Edge TTS 做一个“多角色配音机“的踩坑手记
javascript·typescript·electron
Dovis(誓平步青云)4 小时前
浇水提醒刚弹出又消失,植物状态别只存一个百分比
开发语言·前端·javascript·pdf·ecmascript·电脑
码艺-Alimjan4 小时前
Vben Admin 新增维吾尔语 Vben-Modal的关键坑之一
前端·javascript·vue.js
可乐鸡翅yeah_5 小时前
hls.js 手动自定义 http 请求 loader,修改请求头实战
开发语言·前端·javascript·网络协议·http·ecmascript·m3u8在线
IT_陈寒5 小时前
Vite静态资源导入这个坑我帮你们踩过了
前端·人工智能·后端
广州华水科技5 小时前
大坝安全监测解决方案:单北斗GNSS形变监测系统应用与维护
前端
yivifu6 小时前
中文古籍电子书注释集成
前端·javascript·python·beautifulsoup·epub