r/learnjavascript • u/whiskyB0y • Mar 06 '26
Window.location vs Document.location
Beginner in JavaScript here. I'm currently learning web development.
If I want to link a webpage to another webpage using JavaScript and a button, is it better to use window.location or document.location? Take my code for example: Let's say we have page1.html and page 2.html
In page1.html we have
<button onclick="document.location='page2.html'"> Go to page 2 </button>
Now if we want to use window.location.assign, we make a function:
In a js file called "page2.js"
function goToPage2 ( ) { window.location.assign("page2.html") }
In page1.html we have
button onclick="goToPage2()"> Go to page 2 </button>
So which one is better? Is there a method better than both of these?
View all comments
Show parent comment
2
u/warpedspockclone Mar 06 '26
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.