Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
communitycreator

What is window.location.assign() in Javascript?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The window.location.assign() method belongs to the Location object. It allows the window to load a document specified by the provided URLUniform Resource Locator. Once you have navigated from the current window to the new one, you can still go back to the former by pressing the back button.

Syntax

location.assign(url)

// OR
window.location.assign(url)

Parameter

url: this is the URLUniform Resource Locator that will be navigated to.

Code

Example 1

  • Let’s use the window.location.assign() to navigate to "https://educative.io/edpresso".

  • In the example below, we create a navigate() function that will call the window.locaton.assign().

  • An onclick event listener is added to the button to call this function when it is clicked.

Note: An incorrect URL will throw a DOMException error.

Example 2

  • In the example below, we will use an invalid URL, "hps://educative.io/edpresso".
  • This will cause an error to occur and there will be no navigation.

RELATED TAGS

javascript
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring