54

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/

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
user3522457
  • 2,725
  • 6
  • 22
  • 24

4 Answers4

85

Try-

window.location.href = link;

or

window.location.assign(link);

JSFiddle

Check out the syntax of window.location here.

Lawrence Cherone
  • 44,769
  • 7
  • 56
  • 100
Sahil Mittal
  • 20,515
  • 12
  • 60
  • 89
11

Try using:

window.location.href = link;

MDN source

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Yaje
  • 2,589
  • 16
  • 32
1
window.location.href = link;

Use this.

Deepu--Java
  • 3,620
  • 2
  • 15
  • 28
0

Either use:

window.location.href = link;

Or with .replace();:

window.location.replace(link);
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Jai
  • 72,925
  • 12
  • 73
  • 99