javascript 注销时jQuery清除缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6691998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery clear cache on logout
提问by webjay
When users logout from my mobile app, how can I make sure the cache is cleared?
当用户从我的移动应用程序注销时,如何确保清除缓存?
What I'm thinking about is to redirect /logout to a specific page that clears the cache and redirects to the front page, but how do I clear everything from the cache?
我在想的是将 /logout 重定向到清除缓存并重定向到首页的特定页面,但是如何清除缓存中的所有内容?
I'm using jQuery Mobile 1.0b2pre.
我正在使用 jQuery Mobile 1.0b2pre。
采纳答案by webjay
Here's how I solved it:
这是我解决它的方法:
My /logout
action where the users session is destroyed in the backend redirects to /exit
which has an id attribute of exitPage
.
In my JavaScript I have asked jQuery Mobile to trigger when that page is about to be created. I then empty the DOM and redirects to the front page.
我/logout
在后端销毁用户会话的操作重定向到/exit
具有 id 属性的exitPage
. 在我的 JavaScript 中,我已经要求 jQuery Mobile 在即将创建该页面时触发。然后我清空 DOM 并重定向到首页。
/exit:
/出口:
<div data-role="page" id="exitPage"></div>
/my.js:
/我的.js:
jQuery('#exitPage').live('pagebeforecreate', function(){
jQuery(document).empty();
window.location.replace('/');
});
回答by TheBrain
you can't clear the cache. but what you can do is identify the user based on his session id, and append that to the assets urls someimage.png?cachecontrol=blablalba
next time it enters, he will have a new session id so he will get new files even if the old ones are still in the cache. the other solution will be to explicitly set the cache control header to no-cache. but you can't force his browser to clear it's cache
你不能清除缓存。但是您可以做的是根据他的会话 ID 识别用户,并在someimage.png?cachecontrol=blablalba
下次输入时将其附加到资产 url ,他将拥有一个新的会话 ID,因此即使旧文件仍在缓存中,他也会获得新文件. 另一种解决方案是将缓存控制标头显式设置为 no-cache。但你不能强迫他的浏览器清除它的缓存
回答by Michael Mior
To avoid changing the URL of all your pages, you could send an ETag
header with each response in the session based on the session ID. If you also include Cache-Control:must-revalidate
, this should do the trick.
为避免更改所有页面的 URL,您可以ETag
根据会话 ID 为会话中的每个响应发送一个标头。如果您还包括Cache-Control:must-revalidate
,这应该可以解决问题。