国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語言 > JavaScript > 正文

關(guān)于angularJs清除瀏覽器緩存的方法

2024-05-06 15:24:01
字體:
供稿:網(wǎng)友

緩存篇

一個(gè)緩存就是一個(gè)組件,它可以透明地儲(chǔ)存數(shù)據(jù),以便以后可以更快地服務(wù)于請(qǐng)求。多次重復(fù)地獲取資源可能會(huì)導(dǎo)致數(shù)據(jù)重復(fù),消耗時(shí)間。因此緩存適用于變化性不大的一些數(shù)據(jù),緩存能夠服務(wù)的請(qǐng)求越多,整體系統(tǒng)性能就能提升越多。

瀏覽器緩存,有時(shí)候我們需要他,因?yàn)樗梢蕴岣呔W(wǎng)站性能和瀏覽器速度,提高網(wǎng)站性能。但是有時(shí)候我們又不得不清除緩存,因?yàn)榫彺婵赡苷`事,出現(xiàn)一些錯(cuò)誤的數(shù)據(jù)。像股票類網(wǎng)站實(shí)時(shí)更新等,這樣的網(wǎng)站是不要緩存的,像有的網(wǎng)站很少更新,有緩存還是比較好的。

以下是傳統(tǒng)的清除瀏覽器的方法

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的臨時(shí)緩存

<body onLoad="javascript:document.yourFormName.reset()"> 

ajax清除緩存

$.ajax({    url:'www.haorooms.com',    dataType:'json',    data:{},    cache:false,     ifModified :true ,     success:function(response){      //操作    }    async:false  }); 

用隨機(jī)數(shù),隨機(jī)數(shù)也是避免緩存的一種很不錯(cuò)的方法!

URL 參數(shù)后加上 "?ran=" + Math.random(); //當(dāng)然這里參數(shù) ran可以任意取了 

用隨機(jī)時(shí)間,和隨機(jī)數(shù)一樣。

在 URL 參數(shù)后加上 "?timestamp=" + new Date().getTime();  

用php后端清理

在服務(wù)端加 header("Cache-Control: no-cache, must-revalidate");等等(如php中) 

下面介紹關(guān)于angularJs項(xiàng)目中清除瀏覽器的方法,當(dāng)然以上傳統(tǒng)的方法也是可以適用的,但對(duì)于angularJs來說還需添加以下幾項(xiàng):

一、清除模板緩存

.run(function($rootScope, $templateCache) {        $rootScope.$on('$routeChangeStart', function(event, next, current) {          if (typeof(current) !== 'undefined'){            $templateCache.remove(current.templateUrl);          }        });      });  

二、html添加隨機(jī)參數(shù)

.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" rel="external nofollow" > 

三、清除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';             
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 土默特左旗| 勃利县| 拜城县| 肃宁县| 沙雅县| 永新县| 城口县| 新津县| 大石桥市| 德令哈市| 罗平县| 平山县| 大城县| 建水县| 莱阳市| 来安县| 巴彦县| 汉沽区| 梅州市| 舒城县| 剑川县| 肥乡县| 嘉定区| 万源市| 荆门市| 崇文区| 德惠市| 嘉善县| 石城县| 科技| 吉林省| 城市| 绥宁县| 桐庐县| 迁安市| 宁德市| 峡江县| 当雄县| 武胜县| 巴东县| 易门县|