[英]Angular 7 - ngOnInit - window.location.href not working - From HTTPS to HTTP
I have following piece of code in my ngOnInit event.我的 ngOnInit 事件中有以下代码。
ngOnInit()
{
let Url = window.location.href.trim().toLocaleLowerCase();
if (Url.startsWith("https://"))
{
console.log('HTTPS');
let NewUrl = Url.replace("https://", "http://");
this.document.location.href = NewUrl;
}
}
My aim is that if Url is having HTTPS then I need to redirect to same Url but with HTTP. But this piece of code is not working even though code comes to the HTTPS block.我的目标是,如果 Url 有 HTTPS,那么我需要重定向到相同的 Url,但有 HTTP。但是即使代码到达 HTTPS 块,这段代码也不起作用。 No error or warning is shown either.
也没有显示错误或警告。 I already have tried using
我已经尝试过使用
I am using我在用
How to achieve my purpose?如何达到我的目的?
Thanks in advance,提前致谢,
I had to do this in IIS.我必须在 IIS 中执行此操作。 No solution worked n JavaScript.
JavaScript 没有解决方案。
you should consider a bit cleaner if condition -如果有条件,您应该考虑更清洁一些-
if (window.location.protocol === 'https:') {
const correctProtocolUrl = window.location.href.trim().toLocaleLowerCase().replace('https://', 'http://');
window.location.href = correctProtocolUrl;
}
I've tested this in angular 13, but expecting similar behaviour since angular should not do anything with window.我已经在 angular 13 中对此进行了测试,但期望自 angular 以来的类似行为不应该对 window 做任何事情。
But if you have backend solution I think that is the best way this can be done.但是如果你有后端解决方案,我认为这是最好的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.