1.需求
我想在項目中訪問一個html 文件 這個html 文件中 跳轉的又是另一個地址 。這個需求是為了讓實現公眾號H5 的重定向登錄? 主要是 一個oem 系統 他有多個公眾號 但是每個公眾號 都有對應不用的域名?
2.動態域名
<!DOCTYPE html>
<html lang="zh"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Document</title>
</head><body></body></html>
<script>window.onload = function () {var currentUrl = window.location.href;// 使用URL對象解析URL var urlObj = new URL(currentUrl);// 提取協議和域名部分 var baseUrl = urlObj.origin;// 輸出或重定向到新的URL console.log(baseUrl); // 僅用于演示,打印到控制臺 window.location.href = `${baseUrl}/#/pages/login/index/${location.search}`;};
</script>
3.總結?
這樣寫 相當于 先獲取了html 里的 路徑地址? 取出來了 域名 因為這個域名 是服務器地址 也是公眾號 配置的的域名 這樣寫就巧妙的實現了動態地址