瀏覽器緩存,有時候我們需要他,因爲他可以提高網站性能和瀏覽器速度,提高網站性能。但是有時候我們又不得不清除緩存,因爲緩存可能誤事,出現一些錯誤的數據。像股票類網站實時更新等,這樣的網站是不要緩存的,像有的網站很少更新,有緩存還是比較好的。
以下是傳統的清除瀏覽器的方法
meta方法
//不緩存
<META HTTP-EQUIV="pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<META HTTP-EQUIV="expires" CONTENT="0">
清理form的臨時緩存
<body onLoad="javascript:document.yourFormName.reset()">
ajax清除緩存
$.ajax({
url:'www.haorooms.com',
dataType:'json',
data:{},
cache:false,
ifModified :true ,
success:function(response){
//操作
}
async:false
});
URL 參數後加上 "?ran=" + Math.random(); //當然這裏參數 ran可以任意取了
在 URL 參數後加上 "?timestamp=" + new Date().getTime();
在服務端加 header("Cache-Control: no-cache, must-revalidate");等等(如php中)
下面介紹關於angularJs項目中清除瀏覽器的方法,當然以上傳統的方法也是可以適用的,但對於angularJs來說還需添加以下幾項:
一、清除模板緩存
.run(function($rootScope, $templateCache) {
$rootScope.$on('$routeChangeStart', function(event, next, current) {
if (typeof(current) !== 'undefined'){
$templateCache.remove(current.templateUrl);
}
});
});
二、html添加隨機參數
.state("content", {
url: "/",
views:{
"bodyInfo":{templateUrl: 'tpls/bodyInfo.html?'+ +new Date(),
controller:'bodyInfoCtrl'},
"header":{templateUrl: 'tpls/header.html?'+ +new Date(),
controller:'headerCtrl'
},
"footer":{templateUrl: 'tpls/footer.html?'+ +new Date(),
controller:'footerCtrl'
}
}
})
<link rel="stylesheet" href="stylesheets/main.css?version=1.0.3">
三、清除route緩存
.config(['$stateProvider', '$urlRouterProvider','$locationProvider','$httpProvider',function($stateProvider, $urlRouterProvider,$locationProvider,$httpProvider) {
// $urlRouterProvider.when("", "/home");
$urlRouterProvider.otherwise('/');
if (!$httpProvider.defaults.headers.get) {
$httpProvider.defaults.headers.get = {};
}
$httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
$httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
$httpProvider.defaults.headers.get['Pragma'] = 'no-cache';
好了……就這麼多了
如果還有其他方法歡迎指點迷津!