gpt4 book ai didi

Javascript:重定向到不同的子域

转载 作者:行者123 更新时间:2023-12-02 13:55:43 24 4
gpt4 key购买 nike

我有一个登陆页面:“https://www.example.com ”和一个应用程序:“https://app.example.com/something/ ”,并且在某些用户操作(例如注销)中,我需要将用户重定向回原始登陆页面。我的硬编码解决方案确实有效:

window.location = "https://www.example.com"

但这是不好的做法,并且在其他环境中很烦人,例如我的本地开发(http://localhost:8082/something - 对于应用程序,没有本地登陆页面)。是否有一些好的且简单的解决方案可以重定向到我当前网站的基域(默认情况下 example.com 重定向到 www.example.com)?

最佳答案

然后我会使用

var hostname = location.hostname;
window.location = `https://${hostname}`;

来源: Determine domain name in JavaScript?

还有: http://www.w3schools.com/jsref/prop_loc_hostname.asp

编辑:对于端口:

http://www.w3schools.com/jsref/prop_loc_port.asp

你可以使用

var hostname = location.hostname;
var port = location.port;
window.location = `https://${hostname}:${port}`

关于Javascript:重定向到不同的子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40723881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com