js清除浏览器缓存 2017年11月28日11:31:27一个缓存就是一个

  关于angularJs清除浏览器缓存的方法

  更新时间:2017年11月28日 11:31:27 作者:狼牙冷

  这篇文章主要介绍了关于angularJs清除浏览器缓存的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  缓存篇

  一个缓存就是一个组件,它可以透明地储存数据,以便以后可以更快地服务于请求。多次重复地获取资源可能会导致数据重复js清除浏览器缓存,消耗时间。因此缓存适用于变化性不大的一些数据,缓存能够服务的请求越多,整体系统性能就能提升越多。

  js 清除浏览器缓存_清除浏览器缓存 js_js清除浏览器缓存

  浏览器缓存js清除浏览器缓存,有时候我们需要他,因为他可以提高网站性能和浏览器速度,提高网站性能。但是有时候我们又不得不清除缓存,因为缓存可能误事,出现一些错误的数据。像股票类网站实时更新等,这样的网站是不要缓存的,像有的网站很少更新,有缓存还是比较好的。

  以下是传统的清除浏览器的方法

  meta方法

  <pre class="brush:xhtml;">
//不缓存

</pre>

  清理form的临时缓存

  js 清除浏览器缓存_js清除浏览器缓存_清除浏览器缓存 js

  <pre class="brush:xhtml;">
</pre>

  ajax清除缓存

  <pre class="brush:js;">
$.ajax({
url:'www.haorooms.com',
dataType:'json',
data:{},
cache:false,
ifModified :true ,

success:function(response){

 //操作 

}
async:false
}); </pre>

  用随机数,随机数也是避免缓存的一种很不错的方法!

  URL 参数后加上 "?ran=" + Math.random(); //当然这里参数 ran可以任意取了

  清除浏览器缓存 js_js清除浏览器缓存_js 清除浏览器缓存

  用随机时间,和随机数一样。

  在 URL 参数后加上 "?timestamp=" + new Date().getTime();

  用php后端清理

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

  下面介绍关于angularJs项目中清除浏览器的方法,当然以上传统的方法也是可以适用的,但对于angularJs来说还需添加以下几项:

  js清除浏览器缓存_js 清除浏览器缓存_清除浏览器缓存 js

  一、清除模板缓存

  <pre class="brush:js;">
.run(function($rootScope, $templateCache) {

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

  二、html添加随机参数

  <pre class="brush:js;">
.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' 
      } 
    } 
  }) </pre>

  <pre class="brush:xhtml;">
</pre>

  js 清除浏览器缓存_js清除浏览器缓存_清除浏览器缓存 js

  三、清除route缓存

  <pre class="brush:js;">
.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'; </pre>

  好了……就这么多了

  如果还有其他方法欢迎指点迷津!

  以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/1612
0 评论
398

发表评论

!