Use the assign() or replace() methods to lets the user go to a new URL. You can replace the current URL value to go to another URL In JavaScript.
JavaScript go to URL Examples
HTML examples code:
Assigning a new value to window.location
To navigate to a new URL, use the location object from the Browser’s History API.
Syntax
window.location = "url";
Complete code: will redirect us to a new URL
<!DOCTYPE HTML>
<html>
<body>
<script>
function goTo(){
console.log("click")
window.location = ('https://eyehunts.com')
console.log(window.location)
}
goTo();
</script>
</body>
</html>
Using the window.assign() method
The assign method assigns the current URL with the assigned URL and adds it to the history stack.
window.location.assign('https://eyehunts.com');
Using the window.replace() method
Using the replace() method, you can navigate a user to a site and stop them from going back to the previous page.
window.location.replace('https://eyehunts.com');
Output: For all examples
Do comment if you have any doubts and suggestions on this JS URL topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version
Appreciate this post. Let me try it out.