those. there is a site where all the section pages are HTML files that are loaded by AJAX, but here’s the problem if you open any section of the site and then refresh the page, you will see the main page, and this is logical, but how to get out of this situation

How can I save AJAX-loaded data after refreshing the page?

    3 answers 3

    Change the address in the address bar using window.history.pushState

    Here's an article about this: HTML5: Changing the browser-URL without refreshing page .

    Here is another good example of code, just for your case: Modify the URL without reloading the page .

    • Thanks, but I need that after pressing f5 or CTRL + f5, the page does not lose what it was, loaded by Ajax. If I change the URL and use window.history, then when I press f5, my URL is processed by the web server, i.e. it searches for the contacts. * file with the URL == and if it does not find it returns 404, and if the file name and alias match, then it loads the contents of this file - deniz
    • four
      Aftar burn further! Read what you wrote! - Artem
    • "WITHOUT reloading the page", i.e. all this will work before I press F5 and then it is 404, because the URL is made at the client and the server does not know anything about it. - deniz
    • 2
      @deniz: Well, make up a URL that will be understood by the server, business! - VladD

    You can store data in sessionStorage you can read more here and here . As an option, I propose to upload ajax data every time. To do this, you need to mark the URL with a hash like http: // site / # post / 123, then at the next page load the data from window.location.hash . With jQuery, it looks like this:

     $(document).ready(function(){ url = window.location.hash.substr(1) //убираем символ # $("#content").load(url) //загружаем данные аяксом в контейнер #content }) 

      No After the user has updated the page, the browser will try to load everything in a new way (well, or pull it out of the cache).

      As correctly suggested, you can modify the url in the address bar. Then clicking refresh will load the page from this new address. But then you need to make sure that the content that you load dynamically and which is requested in a new way coincides.

      • I had to use localStorage and refuse to change the URL, because htaccess can not be used. - deniz