
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to remove the hash from window.location (URL) with JavaScript without page refresh?
The replaceState() method replaces the current history entry with the state objects, title, and URL passed as method parameters. This method is useful when you want to update the current history entry's state object or URL in response to a user action.
To remove the hash URL, use the history API's replaceState method to remove the hash location.
Syntax
The syntax for the replaceState() method is as follows −
window.history.replaceState(stateObj,"unused", "url")
The function will take three parameters. They are −
stateObj − This parameter is associated with history entry which is passed to the replace method. This parameter can be null.
URL − ReplaceState throws an exception if the new URL is not of the same origin as the current URL.
Example 1
This example demonstrates how the hash is removed from URL in JavaScript −
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scal=1.0"> <title>Remove Hash from URL</title> </head> <body style="text-align: align-self: start;"> <h1 style="color: orange">Tutorials Point</h1> <p>Removing the hash from window.location with JavaScript without page refresh </p> <p>modifying the current history state can also be done </p> <button onclick="modState()"> History state Modification </button> <button onclick="remHash()"> Remove hash from url </button> <script> function modState() { let stateObj = { id: "100" }; window.history.replaceState(stateObj, "Untitled-1", "/answer#Untitled-1.html"); } function remHash() { var uri = window.location.toString(); if (uri.indexOf("#") > 0) { var clean_uri = uri.substring(0, uri.indexOf("#")); window.history.replaceState({}, document.title, clean_uri); } } </script> </body> </html>
- Related Articles
- Remove elements from Javascript Hash Table
- Remove duplicates from array with URL values in JavaScript
- How to refresh a page in Firefox?
- How to refresh a browser then navigate to a new page with Javascript executor in Selenium with python?
- How to check a URL contains a hash or not using JavaScript?
- How to refresh a JSP page at regular interval?
- How to remove the title bar in a Tkinter window without using overrideredirect() method?
- HTML DOM Location hash Property
- How to get the current URL with JavaScript?
- How to get the title and URL of the page in Selenium with python?
- How to Automatic Refresh a web page in a fixed time?
- How to copy from clipboard using tkinter without displaying a window
- How to display JavaScript variables in an HTML page without document.write?
- How to create a hash from a string in JavaScript?
- How to remove options from a dropdown list with JavaScript?
