59

Consider:

$('.b').click(function(){
    var link = 'www.yahoo.com';
    window.location.href(link);
});

I expect it to open www.yahoo.com, but it says "string is not a function". Why?

jsFiddle: http://jsfiddle.net/V9Xat/

1

4 Answers 4

95

Try-

window.location.href = link;

or

window.location.assign(link);

JSFiddle

Check out the syntax of window.location here.

Sign up to request clarification or add additional context in comments.

3 Comments

Come on.. Explanation, link to API... Something ...
@FlorianF., of course! :)
It is worth noting that window.location.href can be used as a function in both IE and Edge, just not other browsers (and it isn't the standard).
11

Try using:

window.location.href = link;

MDN source

Comments

1
window.location.href = link;

Use this.

Comments

0

Either use:

window.location.href = link;

Or with .replace();:

window.location.replace(link);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.