Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

231
Visualizações
Angular 7 - ngOnInit - window.location.href not working - From HTTPS to HTTP

I have following piece of code in my ngOnInit event.

  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. No error or warning is shown either. I already have tried using

  1. document.location.href
  2. location.href
  3. this.router.navigateByUrl
  4. Injecting DOCUMENT and using this.document.location.href

I am using

  • Angular: 7.1.0
  • Angular CLI: 7.1.4
  • Node: 10.15.3
  • OS: win32 x64
  • @angular/router: 7.1.4

How to achieve my purpose?

Thanks in advance,

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I had to do this in IIS. No solution worked n JavaScript.

about 4 years ago · Juan Pablo Isaza Relatório

0

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.

But if you have backend solution I think that is the best way this can be done.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda