【前端】进入项目时自动跳转到指定链接

文章目录

背景:一个老的项目,被重构后,希望通过以前老项目的域名自动跳转到重构后的新项目域名。

  1. 找到index.html根文件
  1. 在HTML文件head头部添加url自动跳转
html 复制代码
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>后台管理系统</title>
        <link rel="stylesheet" href="index.css" />
        <script>
        	// 鉴别是否为老版测试环境,是true表测试地址,不是false表线上地址。
            const debug = window.location.origin === "https://old-test.com"
            window.location.href = debug ? 'https://new-test.com' : 'https://new-prod.com';
        </script>
    </head>
    <body>
        <div id="root"></div>
    </body>
</html>
  1. 在新版上提供旧版项目的入口,并使其不自动跳转
shell 复制代码
-. 原理如下:
1. 指定一个搜索参数from=v2(表示是从新版切换回旧版)
2. 鉴别from是否等于v2(用于判断是否取消自动跳转)
html 复制代码
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>后台管理系统</title>
        <link rel="stylesheet" href="index.css" />
        <script>
        	// 鉴别是否为老版测试环境,是true表测试地址,不是false表线上地址。
            const debug = window.location.origin === "https://old-test.com"
            // 获取 from 搜索参数值
            const from = new URLSearchParams(location.search).get('from')
			// 如果不是从新版切回来的,则自动跳转
            if (from !== 'v2') {
                window.location.href = debug ? 'https://new-test.com' : 'https://new-prod.com';
            }
        </script>
    </head>
    <body>
        <div id="root"></div>
    </body>
</html>
js 复制代码
-. 效果如下
1. 进入 https://old-test.com 会自动跳转到 https://new-test.com
2. 进入 https://old-prod.com 会自动跳转到 https://new-prod.com
3. 进入 https://old-test.com/?from=v2#/ 不会发生自动跳转
4. 进入 https://old-prod.com/?from=v2#/ 不会发生自动跳转
相关推荐
excel1 小时前
为什么在 Three.js 中平面能产生“起伏效果”?
前端
excel2 小时前
Node.js 断言与测试框架示例对比
前端
天蓝色的鱼鱼4 小时前
前端开发者的组件设计之痛:为什么我的组件总是难以维护?
前端·react.js
codingandsleeping4 小时前
使用orval自动拉取swagger文档并生成ts接口
前端·javascript
石金龙5 小时前
[译] Composition in CSS
前端·css
白水清风5 小时前
微前端学习记录(qiankun、wujie、micro-app)
前端·javascript·前端工程化
Ticnix5 小时前
函数封装实现Echarts多表渲染/叠加渲染
前端·echarts
用户22152044278005 小时前
new、原型和原型链浅析
前端·javascript
阿星做前端5 小时前
coze源码解读: space develop 页面
前端·javascript
叫我小窝吧5 小时前
Promise 的使用
前端·javascript