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

文章目录

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

  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#/ 不会发生自动跳转
相关推荐
m0_7381207210 分钟前
CTFshow系列——命令执行web38-40
前端·windows·安全·web安全
是小狐狸呀2 小时前
elementUI-表单-下拉框数据选中后,视图不更新
前端·javascript·elementui
四岁半儿4 小时前
常用css
前端·css
你的人类朋友5 小时前
说说git的变基
前端·git·后端
姑苏洛言5 小时前
网页作品惊艳亮相!这个浪浪山小妖怪网站太治愈了!
前端
字节逆旅5 小时前
nvm 安装pnpm的异常解决
前端·npm
Jerry5 小时前
Compose 从 View 系统迁移
前端
GIS之路6 小时前
2025年 两院院士 增选有效候选人名单公布
前端
四岁半儿6 小时前
vue,H5车牌弹框定制键盘包括新能源车牌
前端·vue.js
烛阴6 小时前
告别繁琐的类型注解:TypeScript 类型推断完全指南
前端·javascript·typescript