Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
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 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda