Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7030815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:43:17+00:00 2026-05-28T00:43:17+00:00

I am trying to modify the content in my page without a reload. Currently

  • 0

I am trying to modify the content in my page without a reload. Currently my code reads:

window.onpopstate = function(event){
    // Ajax Request the Page and replace content with new content
};

This works when I push a state then trigger the popstate event, but if I press the back button in the browser it navigates to the url instead of calling my onpopstate event. How can I prevent a page refresh and update the page with my ajax call instead?

edit: I am trying to update with pushState and popstate. I was hoping to keep my urls hash free.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:43:18+00:00Added an answer on May 28, 2026 at 12:43 am

    You have to make sure there is always a history state you’ve pushed from the current page on the history to prevent the back button from performing a page load.

    If you’re trying to keep the user “contained” in your web app so the back button always provides some kind of function, you need to push at least two states onto the stack and then make sure to push another state from your popstate handler.

    var foo = {foo: true}; // state object
    history.pushState(foo, "unused argument", "#newInitialUri");
    ...
    var bar = {bar: true}
    history.pushState(bar, "unused argument", "#newStateOfWebApp");
    ...
    window.onpopstate = function(event){
        ...
        var baz = {baz: true}
        history.pushState(baz, "unused argument", "#baseState");
    };
    

    In the above example say we loaded ‘/’. The script starts executing and the browser window URI changes to ‘/#newInitialUri’ but no page load occurs. Then immediately after, the browser URI changes to ‘/#newStateOfWebApp’ and no page load occurs.

    The user pushes the back button on their browser. Your popstate handler fires. During your handler, event.state equals foo and the browser uri is ‘/#newInitialUri’. No page load occurs. The handler finishes completing, calling history.pushState and now the browser uri is ‘/#baseState’. No page load occurs. If the user clicks back again, your popstate event will fire again, event.state will equal foo (again), the browser uri will be ‘/#newInitialUri’ (no page load) and then it will be ‘/#baseState’ again (no page load).

    The important thing to remember is that the event.state in your popstate handler always contains the state object for the URI you’ve just come back to, not the one you just came from. This was confusing to me at first, but made sense when I thought about it. For example, the user may have just come back to your page after perhaps having gone off to Google. The state object is your opportunity to communicate the status of your app to your code.

    Keep in mind that some browsers fire the popstate event on page load (which is what’s supposed to happen according to the spec from my understanding). Be sure to check for your state object in your handler before executing your code to make sure you don’t run code you don’t intend to on a page load.

    One final note: if you’re using jQuery to handle events, you’ll need to use event.originalEvent.state to refer to the state object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to modify my GreaseMonkey script from firing on window.onload to window.DOMContentLoaded, but
I'm trying to modify the class of an element if an ajax call based
I'm currently trying to modify some HttpWebRequest functions, but I can't do it through
Hi I'm trying to modify a web page so that it loads faster. Since
I've been trying to modify the AuditTrail code so that it does not copy
The content I am trying to modify has a series of <div> entries, and
I have the following code. I am trying to prevent my page from resubmitting
I'm trying to modify the HTML markup of a page in order to add
I'm trying to modify the content of the response from some actions in Symfony
I'm trying to display a list of titles in a modify/delete page. They load

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.