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

文章目录

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

  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#/ 不会发生自动跳转
相关推荐
葱头的故事20 分钟前
vant van-uploader上传file文件;回显时使用imageId拼接路径
前端·1024程序员节
Mintopia1 小时前
🇨🇳 Next.js 在国内场景下的使用分析与实践指南
前端·后端·全栈
Mintopia1 小时前
深度伪造检测技术在 WebAIGC 场景中的应用现状
前端·javascript·aigc
BUG_Jia1 小时前
如何用 HTML 生成 PC 端软件
前端·javascript·html·桌面应用·1024程序员节
木易 士心1 小时前
CSS 样式用法大全
前端·css·1024程序员节
012925201 小时前
1.1 笔记 html 基础 初认识
前端·笔记·html
2501_929382652 小时前
[Switch大气层]纯净版+特斯拉版 20.5.0大气层1.9.5心悦整合包 固件 工具 插件 前端等资源合集
前端·游戏·switch·1024程序员节·单机游戏
非凡ghost2 小时前
Tenorshare 4DDiG(数据恢复软件) 最新版
前端·javascript·后端
非凡ghost2 小时前
WinMute(自动锁屏静音软件) 中文绿色版
前端·javascript·后端
7ayl2 小时前
Vue3 - Reactivity的核心流程
前端·vue.js