bootstrap的缓存问题怎么处理


本文摘自PHP中文网,作者angryTom,侵删。

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

  如果你想了解更多关于Bootstrap的知识,可以点击:Bootstrap框架

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

1

2

3

$("#myModal").on("hidden.bs.modal", function () {

       $(this).removeData("bs.modal");

   });

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

1

2

3

4

5

6

7

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的:

1

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

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

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

$(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的缓存问题怎么处理的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

使用bootstrap框架有什么好处?

bootstrap面板怎么用

bootstrap 不显示图标怎么办

web 开发里程碑时刻:bootstrap 宣布放弃支持 ie

bootstrap被淘汰了么

为什么前端不用bootstrap

bootstrap是响应式的吗

bootstrap模态框有什么用

bootstrap与angularjs区别

浅谈bootstrap3和bootstrap4的差异

更多相关阅读请进入《bootstrap》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...