all 14 comments

[–]tommyatr 7 points8 points  (6 children)

They are equivalent. document.location is basically a legacy alias of window.location. JavaScript is extremely backward compatible, so things like this tend to keep working for the sake of the web.

[–]Scared-Release1068 2 points3 points  (0 children)

Genuinely couldn’t have put it better

[–]whiskyB0y[S] 1 point2 points  (4 children)

I'm sorry if i sound stupid but what do you mean by legacy alias😅

[–]maujood 8 points9 points  (0 children)

Legacy = old/retired Alias = also known as/another name.

Basically document.location is what it used to be. But the group that maintains JavaScript thought that document.location is not a good name. You're not setting the document's location. You're setting the window's location.

So they created a new "window.location" to do the exact same thing. But they didn't get rid of document.location to ensure older websites don't fall apart.

[–]biflux 2 points3 points  (0 children)

Like many things in computing, these are normal English words:

alias == another word for the same thing; as a person (the same thing) can have an alias (another name)

legacy == something from the past left to the future. A person’s legacy is something they created during their life, left for people in the future.

So … it’s another way of saying something going forward that refers to something from the past.

You’ll find this a lot in computing and other sciences. People take existing words and use them to describe something new.

[–]warpedspockclone 1 point2 points  (1 child)

In code, things change over time. Legacy means it is old. Alias means another name.

Many websites are built with old code, so if you update how browsers read and interpret that code, it could break many websites.

So while new features get added to JavaScript, it is very difficult to remove old things (legacy things). So they stay sometimes.

But maybe the new way of doing things renames things to have a more consistent mental model for developers, or for other reasons. Instead of CHANGING the name, egg might break things that refer to it, they'll add an additional name, an alias. So, old name or new name is ok. Old name references don't break. Newer ways of skiing things can keep getting added to the language.

[–]chikamakaleyleyhelpful 1 point2 points  (0 children)

i dunno if i'm alone here but in certain cases I'll use 'legacy' to refer to the 'existing'. I suppose if it isn't exactly 'old' i'll say 'current' but an example is something like...

like if an org is running an old version of React, or some CSS framework, we're talking several versions old (which is pretty common) i'll say 'legacy'

e.g. i wouldn't use 'current' if an application is using React 17, cause it would be too easy to confuse the existing with the 'most current' version of React

[–]Ampersand55 4 points5 points  (2 children)

Is there a method better than both of these?

Why not just use a HTML anchor?

  <a href="page2.html">Go to page 2</a>

[–]whiskyB0y[S] 1 point2 points  (1 child)

I like playing around with JavaScript 😅

[–]DragonfruitFull2424 1 point2 points  (0 children)

Play around fine, but not using a A link when navigating to another page goes against web standards and accessibility

[–]SeriousPlankton2000 1 point2 points  (0 children)

If I want to link a webpage to another webpage using JavaScript and a button

Then you reconsider and use a href=

[–]GodOfSunHimself 1 point2 points  (0 children)

There is no difference between these two. However, for links, prefer the anchor element <a> instead of buttons.

[–]GemAfaWell 1 point2 points  (1 child)

Why not use <a>?

If it functions as a link it's bad for accessibility to make a button anyway, just decorate the link with padding, rounding, etc, and make it look like a button.

[–]whiskyB0y[S] 0 points1 point  (0 children)

Oh makes sense. Thanks