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

文章目录

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

  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#/ 不会发生自动跳转
相关推荐
lijun_xiao20095 小时前
前端最新Vue2+Vue3基础入门到实战项目全套教程
前端
90后的晨仔5 小时前
Pinia 状态管理原理与实战全解析
前端·vue.js
杰克尼5 小时前
JavaWeb_p165部门管理
java·开发语言·前端
90后的晨仔6 小时前
Vue3 状态管理完全指南:从响应式 API 到 Pinia
前端·vue.js
90后的晨仔6 小时前
Vue 内置组件全解析:提升开发效率的五大神器
前端·vue.js
我胡为喜呀6 小时前
Vue3 中的 watch 和 watchEffect:如何优雅地监听数据变化
前端·javascript·vue.js
我登哥MVP6 小时前
Ajax 详解
java·前端·ajax·javaweb
非凡ghost7 小时前
Typora(跨平台MarkDown编辑器) v1.12.2 中文绿色版
前端·windows·智能手机·编辑器·软件需求
馨谙7 小时前
/dev/null 是什么,有什么用途?
前端·chrome
JamSlade8 小时前
流式响应 sse 系统全流程 react + fastapi为例子
前端·react.js·fastapi