56

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,738
  • 21
  • 105
  • 131
user3522457
  • 2,845
  • 6
  • 23
  • 24

4 Answers4

92

Try-

window.location.href = link;

or

window.location.assign(link);

JSFiddle

Check out the syntax of window.location here.

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
11

Try using:

window.location.href = link;

MDN source

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yaje
  • 2,753
  • 18
  • 32
1
window.location.href = link;

Use this.

Deepu--Java
  • 3,742
  • 3
  • 19
  • 30
0

Either use:

window.location.href = link;

Or with .replace();:

window.location.replace(link);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jai
  • 74,255
  • 12
  • 74
  • 103