Skip to main content

Explore your training options in 10 minutes

JavaScript: Go to URL: A Complete Guide

Christina Kopecky - January 18, 2021

The window.location value represents the current URL you are viewing in your browser. You can replace this value to go to another URL In JavaScript. This is useful if you want to redirect a user to another page. You can also use the assign() or replace() methods.

There are a couple of reasons you may want to redirect a user to a different website or an updated path name. In most cases, you may want to redirect depending on a user’s authorization – whether a client is logged in to a site. In this article, we take a look at how to do that in a web page using JavaScript.

JavaScript Go to URL

The JavaScript browser history API lets you go to a new URL. You can use the following methods to navigate to a new URL:

Get offers and scholarships from top coding schools illustration

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses

By continuing you agree to our Terms of Service and Privacy Policy , and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

  • Assigning a new value to window.location.
  • Using the window.assign() method.
  • Using the window.replace() method.

All of these three methods accomplish the goal of navigating to another URL equally. Let’s take a look at each of these methods.

JavaScript window.location Value

To navigate to a new URL, use the location object from the Browser’s History API. The session history lets you reassign the location object to a new URL or use the href property on that same object.

The syntax for this approach is:

window.location = "url";

“url” represents the URL you want the user to visit. When this line of code runs, a JavaScript redirect starts. This changes the page which the user views in their web browser.

Let’s take a look at this method in the form of an example:

const handleClick = (e) => {
      window.location = ('') 
      // window.location.href = ('')

When the handleClick() function runs, our code will log a statement to the JavaScript console . Then, our code will redirect us to a new URL.

JavaScript window.location.assign()

The location object has a redirect method called assign(). This method assigns the current URL with the assigned URL and add it to the history stack.

The history stack represents the pages you have viewed (think about the “back arrow” that lets you go back a page).

Consider the following syntax:


Unlike the last example, we do not need to assign any value to a JavaScript variable (window.location). Instead, we use a method to change the web page the user views.

The history stack is how the browser remembers where a back button or a forward button should go to.

Venus, a software engineer at Rockbot

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

Let’s take a look at a full example of this method:

const handleClick = (e) => {

If you want to redirect to a different page on the same site, use the path name property on the location object:

const handleClick = (e) => {
      window.location.pathname = ('/newpage.html')

JavaScript window.location.replace()

You have the ability to control whether the user can use the back button to go back to the previous site.

Using the replace() method, you can navigate a user to a site and stop them from going back to the previous page. The assign() method, on the other hand, saves the page you were previously viewing in your browser history. So, with assign() you can view the last page you were on.

Let’s take a look at the replace() method:


Like assign(), replace() is a method. This method accepts one argument:A the URL to which you want to point the user.

We can use the replace() method with a custom function to change the URL the user views when the method is run:

const handleClick = (e) => {

Use this to play with the different methods to redirect to a URL of your choice. You can use the Run button to reset the code to the default.


There are three ways to go to a URL in JavaScript: using window.location, the window.location.assign() method, or the window.location.replace() method.

Do you want to learn more about JavaScript? Check out our How to Learn JavaScript guide . This guide contains top tips on how to learn JavaScript and a list of comprehensive learning resources for beginners and intermediate developers.

About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.

What's Next?

Christina Kopecky

About the author: Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy,, and Educative.

Skip to main content