4

I'm having trouble redirecting my app outside of Angular to the logout page. I'm doing it by using $window.location.href but it's not working on FireFox. So, There was a suggestion to use $window.location directly but since i'm writing in Typescript i need to create a new Location object rather then just assign my string to it...

I looked into lib.d.ts and i saw that location is declared as:

declare var Location: {
    prototype: Location;
    new(): Location;
}

so i adjust my code to:

var url: string = "http:\\\\host:port/blabla/logout";
var loc: Location = new Location();
loc.href = url;
this.$window.location = loc;

but got this error:

Error: Illegal constructor.

Any Idea how to create the Location object? Is it a good practice to do so? Any other insights maybe?

Thanks

Rivi
  • 791
  • 3
  • 15
  • 23

2 Answers2

5

It actually looks like you're not supposed to build new Location objects. So I don't think there's a constructor available anywhere for this. Though you can use anchors to build them for you.

If you're trying to set a new location, you can go with setting window.location.href instead of window.location (These two are equivalent).

Community
  • 1
  • 1
rtpg
  • 2,419
  • 1
  • 18
  • 31
4

If you want to assign a String directly and just make TypeScript go along with that, you can do

 window.location = <any>"http://host:port/blabla/logout";
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • Not sure if you can create a Location object at all. The DOM documentation just mentions that you can get it from `window.location` and `document.location` and that you can assign **a string** to it. – Thilo Aug 31 '15 at 10:19
  • Why is this the accepted answer? I need a second location object, manipulate some properties and use the resulting url without changing the current location. – SuperNova Aug 28 '20 at 06:30