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

文章目录

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

  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#/ 不会发生自动跳转
相关推荐
0***R51521 分钟前
前端构建工具缓存,node_modules
前端·缓存
坚持就完事了27 分钟前
CSS-4:CSS的三大特性
前端·css
坚持就完事了37 分钟前
CSS-3:背景设置
前端·css·html
坚持就完事了39 分钟前
CSS-2:CSS的元素显示模式
前端·css
肠胃炎1 小时前
Flutter 基础组件
前端·flutter
酥风1 小时前
AI概念解惑系列 - RAG
前端·llm·aigc
IT_陈寒1 小时前
Redis深度优化:10个让你的QPS提升50%的关键配置解析
前端·人工智能·后端
Hilaku1 小时前
别再吹性能优化了:你的应用卡顿,纯粹是因为产品设计烂🤷‍♂️
前端·javascript·代码规范
驯狼小羊羔1 小时前
学习随笔-hooks和mixins
前端·javascript·vue.js·学习·hooks·mixins
r***86981 小时前
Redis 6.2.7安装配置
前端·数据库·redis