Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

234
Views
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 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!