简体   繁体   English

浏览器返回上一页功能,如果返回则如何重新加载文件?

[英]Browser go back to previous page function, how to reload a document if go back?

Here is my problem with browser-build-in go back function. 这是我的浏览器内置返回功能的问题。

Say I have two pages: Page1 and Page2. 假设我有两个页面:Page1和Page2。

What Page1 does is that it downloads a .json file then prints .json file's data on the webpage. Page1的作用是先下载.json文件,然后在网页上打印.json文件的数据。

Page2 allows user to insert new data to that .json file. Page2允许用户将新数据插入该.json文件。

If, user hit page1 then go to page2 to insert new data, then hit the go back button. 如果用户单击第1页,则转到第2页以插入新数据,然后单击“返回”按钮。 The .json file will not updated. .json文件不会更新。 Thus the just-inserted-data wouldn't show. 因此,不会显示刚刚插入的数据。

But I need new data updated. 但是我需要更新新数据。

Any idea to solve this problem? 有解决这个问题的主意吗?

If caching is your issue then you might want to consider this buggy, hoary old chestnut. 如果缓存是您的问题,那么您可能想要考虑这个多虫的,古老的栗子。 Add these to your <head> section, but be wary as its effectiveness varies between browsers/versions. 将它们添加到您的<head>部分,但要小心,因为其有效性在浏览器/版本之间会有所不同。 See link to w3.org for in-depth details. 有关详细信息,请参见w3.org的链接。

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9 https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9

I presume you use javascript and the onload event to download the .json file and to display its content. 我假设您使用javascript和onload事件下载.json文件并显示其内容。 The problem there is that the onload event isn't triggerd when you use the back button. 问题是使用后退按钮时不会触发onload事件。 You could try to use the window.onpopstate event instead of onload. 您可以尝试使用window.onpopstate事件而不是onload。 The onpopstate event is also invoked when the back button is used to navigate to a webpage. 当使用后退按钮导航到网页时,也会调用onpopstate事件。

function init() {
   //load .json ...
}   
window.onpopstate = init;

It could be that the json data is being cached, so even if you request it again upon arriving on "Page1", you may be reading a cached version of it. 可能是json数据正在缓存中,因此即使您到达“ Page1”时再次请求它,您也可能正在读取它的缓存版本。 That is, if you have successfully avoided the page itself from being cached, the data could still be cached. 也就是说,如果您成功避免了页面本身的缓存,则仍然可以缓存数据。

A simple though hacky way to overcome that type of issue, is to alter the URL of the json data every time it is being loaded. 克服此类问题的一种简单易懂的方法是,每次加载json数据时都要更改其URL。 Eg add a timestamp or some other, ideally unique, portion to the URL: 例如,在URL上添加时间戳或其他一些理想的唯一部分:

myjson.json?timestamp=xxx myjson.json?timestamp = xxx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM