An example of why you might wish to update a url without having to reload a web page is lets say the web page has links or tabs that display a related div for example and you want the clicking of the different tabs to be stored in the browsers history so when the user clicks the back or forward button, instead of navigating to the previous full url, the browser instead navigates to the previous query part of the url that is stored in the history stack. This results in a more accurate/better user experience.
To see in action the javascript code that updates the browser query url in the address bar, click on the different 'tabs' below show green
show red
show purple
and notice how the query string in the browsers address bar changes without the page having to reload.
history.pushState(...)
{ tab: tabName }
(First Argument){ tab: tabName }
) associated with the new history entry.window.onpopstate
event.tab
is the property name.tabName
is the value, which represents the name of the currently active tab (e.g., "green", "red", "purple").""
(Second Argument)""
) or provide a descriptive title for future compatibility."?section=" + tabName
(Third Argument)?section=green
or ?section=red
) without reloading the page."section="
is a query parameter indicating the current section or tab.tabName
is appended to the parameter to reflect the active tab (e.g., green
, red
, or purple
).