侧边栏壁纸
博主头像
龍啓博主等级

行动起来,活在当下

  • 累计撰写 23 篇文章
  • 累计创建 23 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

动态加载css和javascript

龍啓
2023-02-23 / 0 评论 / 0 点赞 / 202 阅读 / 1211 字

动态加载css文件

const loadCss = (url: string, isCache = false) => {
  const 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.head.appendChild(element);
};

动态加载javascript

const loadJs =(
  src: string,
  callback: (this: HTMLScriptElement, ev: Event) => void,
  isCache = false
) => {
  const script = document.createElement("script");
  script.type = "text/JavaScript";
  if (isCache) {
    script.src = src + "?t=" + new Date().getTime();
  } else {
    script.src = src;
  }
  if (script.addEventListener) {
    script.addEventListener("load", callback, false);
  }
  document.head.appendChild(script);
}
0

评论区