现在的位置: 首页 > web前端 > 正文

bootstrap的缓存问题怎么处理

2020年01月04日 web前端 ⁄ 共 1685字 ⁄ 字号 评论关闭

  笔者使用的是bootstrap.js v3.0.0版本,这是VS2017MVC中自带的,使用过程中发现modal加载页面有严重的缓存问题,百度了一下,有很多类似的情况,解决办法基本都是如下两种:

  如果你想了解更多关于Bootstrap的知识,可以点击:Bootstrap在线教程

1、在关闭的时候清除数据:

 $("#myModal").on("hidden.bs.modal", function () {        $(this).removeData("bs.modal");    });

2、修改请求的URL,在请求的URL上加上时间戳。

function remoteUrl(u){u += '&t=' + Math.random(1000)    $.get(u, '', function(data){        $('#remoteModal .modal-body').html(data)    })    $('#remoteModal').modal({show:true,backdrop:false})}

  上边的两个解决办法确实有效,但在IE中,第1种方法无效,第2种方法解决起来太繁琐。

  我又百度到了另一种解决办法,专门针对IE的:

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]//不加的话,IE缓存会捣乱

  该办法是要在服务器端给每个action加上,这样的话,这需要加多少action,那位作者居然嫌弃IE太垃圾了应该退出互联网界。

  好了,吐糟完了,来上我的解决办法:直接修改bootstrap.js文件

  位置在大约在1068行的位置,如下代码:

 $(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {    var $this   = $(this)    var href    = $this.attr('href')    var $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) //strip for ie7    var remoteUrl = !/#/.test(href) && href    if (remoteUrl == undefined) {        remoteUrl = "";    }    if (remoteUrl.indexOf("?") > -1) {        remoteUrl += "&" + (new Date()).valueOf()    }    else {        remoteUrl += "?" + (new Date()).valueOf()    }    //var option  = $target.data('modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data())    //上边的是原代码,增加了remoteUrl来解决IE下缓存的问题    var option = $target.data('modal') ? 'toggle' : $.extend({ remote: remoteUrl }, $target.data(), $this.data())    e.preventDefault()    $target      .modal(option, this)      .one('hide', function () {        $this.is(':visible') && $this.focus()      })  })

注释已经说明了解决办法,我只是增加了remoteUrl,在请求的url后加上时间,这样就不用一个一个的修改,也能兼顾各个浏览器了。

以上就是bootstrap的缓存问题怎么处理的详细内容,更多请关注学步园其它相关文章!

抱歉!评论已关闭.