当前位置: 首页 > article >正文

解决浏览器缓存问题

1.index.html文件meta标签添加属性

<meta name="viewport" content="width=device-width,initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" viewport-fit=cover  >

2.提前main.html处理逻辑再跳转到index.html页

<script>
    // 解决浏览器缓存
    function timestamp(url) {
      const getTimestamp = new Date().getTime()
      if (url.indexOf('?') > -1) {
        url = url + '×tamp=' + getTimestamp + '&loginType=' + loginType
      } else {
        url = url + '?timestamp=' + getTimestamp + '&loginType=' + loginType
      }
      return url
    }
    const getUrlParam = name => {
      const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
      const value = window.location.search.substr(1).match(reg)
      if (value != null) {
        // 对参数值进行解码
        return decodeURIComponent(value[2])
      }
      return null
    }
    const loginType = getUrlParam('loginType')
    // console.log('main.html当前url链接地址:' + window.location.href)
    const url = './index.html'
    const newUrl = timestamp(url, loginType)
    window.open(newUrl, '_self')
</script>

3.动态加载文件

3.1 封装动态加载、移除文件方法
/**
 * 动态加载css文件
 * @param {*} url
 * @param {*} isCache
 */
export function loadCSS(url, isCache = true) {
  let element = document.createElement('link')
  element.setAttribute('rel', 'stylesheet')
  element.setAttribute('type', 'text/css')
  if (isCache) {
    element.setAttribute('href', url + '?t=' + new Date().getTime())
  } else {
    element.setAttribute('href', url)
  }
  document.getElementsByTagName('head')[0].append(element)
  // document.head.appendChild(element)
}

/**
 * 动态加载js文件
 * @param {*} src
 * @param {*} callback
 *   loadScript("",function(){
 *   console.log("加载成功")
 * })
 * var that = this; 在方法里面使用that
 */
export function loadJS(jsUrl, callback, isCache = true) {
  const script = document.createElement('script')
  const head = document.head
  script.type = 'text/JavaScript'
  if (isCache) {
    script.src = jsUrl + '?t=' + new Date().getTime()
  } else {
    script.src = jsUrl
  }
  if (script.addEventListener) {
    script.addEventListener('load', callback, false)
  }
  document.getElementsByTagName('head')[0].append(script)
  // head.appendChild(script)
}

/**
 * 移除引用的JS/CSS文件
 * @param filename 文件名称
 * @param filetype 文件类型 js/css
 */
export const removeFileJSandCSS = (filename, filetype) => {
  const targetElement = filetype === 'js' ? 'script' : filetype === 'css' ? 'link' : 'none'
  const targetAttr = filetype === 'js' ? 'src' : filetype === 'css' ? 'href' : 'none'
  const allSuspects = document.getElementsByTagName(targetElement)
  for (let i = allSuspects.length; i >= 0; i--) {
    if (
      allSuspects[i] &&
      allSuspects[i].getAttribute(targetAttr) != null &&
      allSuspects[i].getAttribute(targetAttr).indexOf(filename) != -1
    )
      allSuspects[i].parentNode.removeChild(allSuspects[i])
  }
}
3.2 页面引入
removeFileJSandCSS('nationality.js', 'js')
loadCSS('./jQuery/jquery-ui.min.css')
loadJS('./jQuery/jquery-ui.min.js', () => {})

http://www.kler.cn/news/156013.html

相关文章:

  • 基于算能的国产AI边缘计算盒子8核心A53丨17.6Tops算力
  • 设置随机种子保证网络可复现性
  • 智能优化算法应用:基于黄金正弦算法无线传感器网络(WSN)覆盖优化 - 附代码
  • 有序表常见题型
  • 2023年GopherChina大会-核心PPT资料下载
  • Fiddler抓包工具之fiddler的常用快捷键
  • SpringAMQP入门案例——发送消息
  • Android CardView基础使用
  • 反序列化漏洞详解(三)
  • blender导出相机参数
  • ESP32使用mpu6050以及pid调参
  • Python - 字典3
  • 详解云WAF:免费GOODWAF归来
  • HarmonyOS—ArkTS中@Observed和@ObjectLink装饰器的嵌套类对象属性变化【鸿蒙专栏-11】
  • C语言实现Cohen_Sutherland算法
  • 构造函数的定义
  • Prometheus+Grafana搭建日志采集
  • Langchain-Chatchat的安装过程
  • C++基础 -35- string类
  • 操作系统·存储器管理
  • 亚信科技AntDB数据库完成中国信通院数据库迁移工具专项测试
  • unordered_map与unordered_set的实现(含迭代器)
  • go使用aes加密算法
  • 【ArcGIS Pro微课1000例】0043:深度学习--框架库安装
  • 【java+vue+微信小程序项目】从零开始搭建——健身房管理平台(3)路由导航卫士、主页实现
  • Jupyter NoteBook未授权访问漏洞
  • 2024年天津财经大学珠江学院专升本专业课报名缴费操作流程
  • KALI LINUX入门
  • LC-1423. 可获得的最大点数(子数组和、枚举)
  • 牛客在线编程(SQL大厂面试真题)