![](/img/trans.png)
[英]Javascript - redirect to another website and call Javascript function after that page loads
[英]JavaScript Redirect to Page Another website based on URL
使用 JavaScript,如何使其重定向到基于 URL 的另一个站点? (示例)如果有人去https://example.com/12345 ,它会将他们重定向到https://example.net/12345 。 如果有人去https://example.com/abc123456 ,它会将他们重定向到https://example.net/abc123456
我怎样才能做到这一点?
您可以为此使用以下代码:
location.href = 'https://example.net/12345';
location.href = 'https://example.net/abc123456';
或为此使用以下代码:
location.replace('https://example.net/12345');
location.replace('https://example.net/abc123456');
在您托管该域的地方,看看您是否可以找到使其成为单页应用程序的东西,或者将所有 url 重写到一个页面的方法,这样它就不会显示 404 not found。 (不确定你怎么能做到这一点,我只用 firebase 主机完成它,它有一种配置方式,所以无论你给它什么 url,它总是显示相同的页面,还有 Z572D4E421E5E6B71111A改变)如果你能做到这一点,这是你需要的代码:
let pathname = location.pathname //if the url is https://example.net/1234, the path name will be /1234
location.href = "https://example.net" + pathname //if you add that to this string, it would be https://example.net/1234
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.