0

As far as I've tested, these two methods work, but I don't know which one is the best, or the differences between them, and that's what I'd like to know.

Here are the two methods:

window.location = 'http://www.google.com';
window.location.assign = 'http://www.google.com';
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
yoda
  • 10,834
  • 19
  • 64
  • 92

3 Answers3

1

The difference is this:

window.location ='' /window.location.replace(''), replaces the current document in the browser and browser history

window.location.assign('') assigns a new document to the browser and history.

Effectively "replace" does not support returning to the previous view in history (potentially useful in some applications). "Assign" allows access to history.

Difference between window.location.assign() and window.location.replace()

Community
  • 1
  • 1
augurone
  • 107
  • 5
1

.assign() is actually a function.

The first is probably the most common.

alcohol
  • 22,596
  • 4
  • 23
  • 21
1

These two methods are equivalent. The first one is clearer to me. The syntax for assign would actually be:

window.location.assign('http://www.google.com');

Rob Sobers
  • 20,737
  • 24
  • 82
  • 111