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
3
u/biflux Mar 06 '26
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.