Set window.location with TypeScript

JavascriptJqueryTypescript

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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');

Solution 2 - Javascript

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');

Solution 3 - Javascript

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: >ts >var myUrl = $link.attr('data-href');; >

Solution 4 - Javascript

Just add href

Like this:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1679941View Question on Stackoverflow
Solution 1 - JavascriptNelsonView Answer on Stackoverflow
Solution 2 - JavascriptNullPoiиteяView Answer on Stackoverflow
Solution 3 - JavascriptTop-MasterView Answer on Stackoverflow
Solution 4 - JavascriptDonn FrederickView Answer on Stackoverflow