最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "Re open last closed tab" causing to show last ajax request content - Stack Overflow

programmeradmin5浏览0评论

I am using HTML 5 history api to save state when ajax requests happen and i provide full html content if user request to same page with none ajax request.

"Re-open last closed tab" feature of browser brings last ajax request content without hitting to server. If browser would request without bring last request content then everything would work without problem. But browser just show last ajax request content.

I have been experienced this on Chrome 17, Firefox 10. (i haven't tried it on ie9 because it has no support history api)

What is well-known solution for this problem ?

Edit: These ajax requests are just "get" request to server.

it is really not possible to demonstrate it in jsfiddle because few reasons. You can demonstrate it in your localhost like below.

Make "get" request to server and pull json objects then push that url into history api like below.

history.pushState(null,null,url);

Then close that tab and click "Re-open last closed tab" feature of your browser. What do you see ? Json response body ? Browser shows it without making request to server, right ?

I am using HTML 5 history api to save state when ajax requests happen and i provide full html content if user request to same page with none ajax request.

"Re-open last closed tab" feature of browser brings last ajax request content without hitting to server. If browser would request without bring last request content then everything would work without problem. But browser just show last ajax request content.

I have been experienced this on Chrome 17, Firefox 10. (i haven't tried it on ie9 because it has no support history api)

What is well-known solution for this problem ?

Edit: These ajax requests are just "get" request to server.

it is really not possible to demonstrate it in jsfiddle because few reasons. You can demonstrate it in your localhost like below.

Make "get" request to server and pull json objects then push that url into history api like below.

history.pushState(null,null,url);

Then close that tab and click "Re-open last closed tab" feature of your browser. What do you see ? Json response body ? Browser shows it without making request to server, right ?

Share Improve this question edited Feb 26, 2012 at 1:28 Freshblood asked Feb 25, 2012 at 16:03 FreshbloodFreshblood 6,44110 gold badges61 silver badges98 bronze badges 5
  • 1 Can you post some code, or a link to a page which demonstrates the issue? – robertc Commented Feb 25, 2012 at 22:29
  • @robertc I updated question and provided demonstration info. – Freshblood Commented Feb 26, 2012 at 1:29
  • 1 Is the question really do browsers that allow reopening a tab, reopening a cached page, or do they rerun the page request, including Javascript? I think the answer to that is, in my experience, you're getting the last state of the page before it was closed; no "reload". At least in Firefox 10 using TabMix Plus. – Jared Farrish Commented Feb 26, 2012 at 1:32
  • Although it doesn't seem to happen with this code, so maybe it depends. Hmm. – Jared Farrish Commented Feb 26, 2012 at 1:38
  • @JaredFarrish I couldn't understand your test on jsfiddle because your havent make ajax request, didnt push anything to history and not possible to close tab in iframe too. Browser does not bring last state of page unfortunality :(. If browser would hit to server then everything would work without problem. But it directly shows cached last ajax request content – Freshblood Commented Feb 26, 2012 at 15:41
Add a ment  | 

3 Answers 3

Reset to default 6

Problem was causing by http response headers. Headers was contain cacheable information for ajax requests so browser was showing that url content from cache without hit to database.

After removing cache params from response headers then browser was able to hit server without brings content from cache.

When you reopen a closed tab, the browser is allowed to reuse the data from cache for the given URL to populate the window. Since the data in cache is from the ajax request response, that's what it uses, and you see the JSON.

So that leads to the question: Why didn't the browser use the HTML from cache when satisfying the ajax request? Browsers use different rules to determine whether to use cached content depending on what they're doing. In this case, it appears Chrome is happy to reuse it when restoring the recently-closed tab, and not when doing the ajax request.

You can correct it by telling the browser to never cache the response. Whether that's desirable depends on your use case.

For instance, inserting these at the top of your file (after the opening <?php tag, of course) makes it not happen for me:

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

It all depends which browser you are using, and which optimisations are enabled.

Google Chrome for instance will keep the page in memory, so when you hit back and it goes to a new site, or when you do re-open closed tab - it will restore the page from memory.

Older/slower browsers will just refresh anything.

Though this shouldn't really be a problem, as your javascript state should be restored as well - it should be exactly the same in every way when they re-open that page.

发布评论

评论列表(0)

  1. 暂无评论