Javascript history.pushState() MethodWhenever history is changed, the challenge is to find out about it. The pushState() method has been called. It is used to monitor alterations to the history of the browser. To monkey-patch window.history when the pushState() method is used. The function history will then have all of our own logic added to it, which will run each time the browser's history is modified. In other words, we can add a record to the web browser's session history stack using the history.pushState() method. SyntaxThe syntax for the pushState() method is as follows: Three parameters are accepted by the pushState() method: 1) state A serializable object is a state. A popstate event is launched whenever you navigate to a different state. Additionally, the popstate event has a state attribute that points to the state object of the history entry. 2) title Today, the majority of browsers ignore this title property. Utilise documen.title property in its place if wewant to update the document's title. In real life, the title parameter is typically passed an empty string. 3) url We can specify the URL for the new history item using the optional url. The method will throw an exception if the URL doesn't have the same origin as the one it is currently using. The web browser won't load the new address when we set it. If the user doesn't give the url, the current URL is used by default. ExamplesThe following examples describe different information with the history.pushstate() method. Example 1: The below example is displayed the basic functionality of the history.pushstate() method. Here we can use the windows location url to get a current link. Use the current url to get the methods data. Output The image shows the data as an output of the history url. Example 2 The below example displays the functionality of the history.pushstate() method. we can use null state, current url, and empty parameter. Output The image shows the data as an output of the history url. Example 3 The below example displays the functionality of the history.pushstate() method. We can get the url path of the required id. If a web page has an available id, then use it as a URL part or tab. Output The image shows the data as an output of the history url. PushState() history in JavaScript illustrationWe will create a straightforward application that displays React, Vue, and Angular tabs. The content of the specified item will be displayed when you click a tab. The URL will also be updated using the history. The pushState() function follows following steps:
Example The following example shows the history.pushState() method with different URL tabs in a single container. Here, we create three tabs with their information. If we click on the tab, the URL displays the tab data. Output The image shows the tab with multiple links for url history. Conclusion To add a record to the session history stack of the web browser, use the history.pushState() method. Next TopicJavaScript Infinity PROPERTY |