Window.history.pushState refreshing the browser

Asked
Active3 hr before
Viewed126 times

9 Answers

windowrefreshingbrowser
90%

If pages are refreshing, then it is caused by other things that you are doing (for example, you might have code running that goes to a new location in the case of the address bar changing).,Connect and share knowledge within a single location that is structured and easy to search.,When the function is working properly, the only thing you should expect to see, is the address in your browser's address-bar change to whatever you specify in your URL.,Please be sure to answer the question. Provide details and share your research!

Hence the need for the function

history.pushState({}, "Results for `Cats`", 'url.html?s=cats');
load more v
88%

In an HTML document, the history.pushState() method adds an entry to the browser's session history stack. , This creates a new browser history entry setting the state, title, and url. , In a sense, calling pushState() is similar to setting window.location = "#foo", in that both will also create and activate another history entry associated with the current document. But pushState() has a few advantages: ,BCD tables only load in the browser

history.pushState(state, title[, url])
load more v
72%

I am working on some javascript code, and using window.History.pushState to load new HTML pages, instead of using href tags. My code (which is working fine) looks like this. , stop other form1 from submitting when submitting form2 using php/javascript or jquery , JavaScript Questions , JavaScript Questions

window.History.pushState({
   urlPath: '/page1'
}, "", '/page1')
load more v
65%

window.history.pushState('', 'New Page Title', '/new-url.php');
75%

The popsate is where I have the problems, I think.,The pushstate history.pushState(category,'',window.location.pathname + '?page=' + category);,We can use other links not associated with the category?,Hi @m3g4p0p Been doing some work with this, things are taking shape its fixed the problem - big thanks

The popsate is where I have the problems, I think.

window.onpopstate = function(event) {
   console.log("location: " + document.location + ", state: " + JSON.stringify(event.state));
};
load more v
40%

If the method invoked was the pushState() method: Remove all the entries in the browsing context's session history after the current entry. If the current entry is the last entry in the session history, then no entries are removed.,after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.,There is no way to clear the session history or to disable the back/forward navigation from unprivileged code. The closest available solution is the location.replace() method, which replaces the current item of the session history with the provided URL. Mozilla,So depending on what you are trying to do, you could make a special case for when first loading the page and push a state (one you will consider your first), and in doing so clear the forward states of the history.

for (i = 0; i < 50; i++) {
   history.pushState({}, '');
}
load more v
22%

The HTML5 History API is definitely the way to go for modern websites, as it accomplishes the task at hand, while also providing additional functionality. You can use either history.pushState() or history.replaceState() to modify the URL in the browser, depending on your needs:,The older Location API is not the best tool for the job, as it reloads the page, however it still allows you to modify the current URL and might be useful when working with legacy browsers. You can modify the URL, using either window.location.href, location.assign() or location.replace():,The arguments for both methods are the same, allowing you to pass a customized serializable state object as the first argument, a customized title (although most browsers will ignore this parameter) and the URL you want to add/replace in the browser's history. Bear in mind that the History API only allows same-origin URLs, so you cannot navigate to an entirely different website.,How do I use JavaScript to modify the URL without reloading the page?

// Current URL: https://my-website.com/page_a
const nextURL = 'https://my-website.com/page_b';
const nextTitle = 'My new page title';
const nextState = {
   additionalInformation: 'Updated the URL with JS'
};

// This will create a new entry in the browser's history, without reloading
window.history.pushState(nextState, nextTitle, nextURL);

// This will replace the current entry in the browser's history, without reloading
window.history.replaceState(nextState, nextTitle, nextURL);
load more v
60%

Unfortunatly, there is no API to get notified about window.history.pushState() and window.history.replaceState(), as the browser will only fire onpopstate when the user navigates.,This library would need to monkeypatch the two functions to add in events or a callback.,From the other hand, if React Router works correctly when I call history.back() or history.go(<N>), I expect to set a specific route that will not cause the full refresh.,The same way I expect that I can set a specific route via window.history. There is a pushState method which can set url.

We have iframe based micro-frontends. There is a need of changing the state of the inside app from outside one without full-reloading of the inside one.
There is <BrowserRouter /> which uses Native History API.

<BrowserRouter />
load more v
48%

I am working on some javascript code, and using window.History.pushState to load new HTML pages, instead of using href tags. My code (which is working fine) looks like this. ,you should provide a valid url instead of a path. So:,Second, you need to alter the PHP script to output a JSON string rather instead of the notification script.,history.pushState({urlPath:'/page2.php'},"",'/page2.php') works exactly like it is supposed to in the latest versions of Chrome, IE and Firefox for me and my colleagues.

I am working on some javascript code, and using window.History.pushState to load new HTML pages, instead of using href tags. My code (which is working fine) looks like this.

window.History.pushState({
   urlPath: '/page1'
}, "", '/page1')

strangely, this fails, ie reloads the browser

window.History.pushState({
   urlPath: '/page2.php'
}, "", '/page2.php')

But this works, content is updated, browser not refreshed ! (notice the URL is absolute and not relative)

window.History.pushState({
   urlPath: 'www.domain.com/page2.php'
}, "", 'www.domain.com/page2.php')
load more v

Other "window-refreshing" queries related to "Window.history.pushState refreshing the browser"