r/learnjavascript • • 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?

0 Upvotes

14 comments sorted by

View all comments

Show parent comment

2

u/chikamakaleyley helpful Mar 06 '26

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