129

I am getting an error with the following TypeScript code:

 ///<reference path='../../../Shared/typescript/jquery.d.ts' />
 ///<reference path='../../../Shared/typescript/jqueryStatic.d.ts' />

 function accessControls(action: Action) {
    $('#logoutLink')
        .click(function () {
            var $link = $(this);
            window.location = $link.attr('data-href');
        });

 }

I am getting an underlined red error for the following:

$link.attr('data-href'); 

The message says:

Cannot convert 'string' to 'Location': Type 'String' is missing property 'reload' from type 'Location'

Does anyone know what this means?

huysentruitw
  • 27,376
  • 9
  • 90
  • 133

5 Answers5

251

window.location is of type Location while .attr('data-href') returns a string, so you have to assign it to window.location.href which is of string type too. For that replace your following line:

window.location = $link.attr('data-href');

for this one:

window.location.href = $link.attr('data-href');
Nelson
  • 49,283
  • 8
  • 68
  • 81
  • 5
    I note that in browsers today, setting `window.location = "some string"` has special behaviour, see here: https://stackoverflow.com/questions/2383401/javascript-setting-location-href-versus-location - see the comments regarding same-site, same-origin and XHR behaviour. – Dai Dec 16 '19 at 05:54
  • While implementing a complex PayPal integration, I noticed a very compelling reason to use `window.location`, instead of `window.location.href`: [it does not require `SAME ORIGIN`](http://javascript.info/tutorial/same-origin-security-policy). – Top-Master Apr 11 '22 at 14:54
44

you have missed the href:

Standard, To use window.location.href as window.location is technically an object containing:

Properties
hash 
host 
hostname
href    <--- you need this
pathname (relative to the host)
port 
protocol 
search 

try

 window.location.href = $link.attr('data-href');
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
7

There's an assign method on the Location interface that plays perfectly well with typescript when passed a string and works the same as window.location = LOCATION.

window.location.assign('http://example.com');
interface Location {
    ...
    /** Navigates to the given URL. */
    assign(url: string | URL): void;
}

The method seems to have be around for a long time (IE 5.5!).

https://developer.mozilla.org/en-US/docs/Web/API/Location/assign

robstarbuck
  • 6,893
  • 2
  • 41
  • 40
3

While implementing a complex PayPal integration, I noticed a very compelling reason to use window.location, that it does not require SAME ORIGIN.

Hence we did something like:

(<any> window).location = myUrl;

Instead of:

window.location.href = myUrl;

Where in OP's case:

var myUrl = $link.attr('data-href');
Top-Master
  • 7,611
  • 5
  • 39
  • 71
-3

Just add href

Like this:

window.location.href = $link.attr('data-href');
  • 2
    This is the same solution as in [the accepted answer](https://stackoverflow.com/a/13106955/2227743). *When answering older questions that already have answers, please make sure you provide either a novel solution or a significantly better explanation than existing answers.* – Eric Aya Sep 20 '21 at 08:00