16

What's the difference between window.location.assign(url) and window.open(url, '_self')?


Related questions:

Ryan Li
  • 9,020
  • 7
  • 33
  • 62
  • What did the related answers not answer for you? https://developer.mozilla.org/en-US/docs/Web/API/Location/assign vs https://developer.mozilla.org/en-US/docs/Web/API/Window/open – mplungjan Mar 22 '17 at 15:24

2 Answers2

4

Functionally? Not much. They do similar things in different ways.

Personally, I'd probably choose window.location over window.open. Even though they do the same things, using window.location for changing the current window is more common, and doesn't require knowing about '_self', which isn't used that often. My experience is my coworkers expect window.open to involve opening a window versus reusing the same one.

Michael Landis
  • 1,074
  • 7
  • 12
-4

window.location.assign will assign the current window a new URL value. window.open will open a new window (which may be in a new tab, or not, depending) with the value of the url passed.

It's the difference between creating a new window and editing an existing window.

Aweary
  • 2,302
  • 17
  • 26
  • "window.open will open a new window" — Not with the arguments that the question has specified. – Quentin May 30 '17 at 16:24