极
极核实验室

Loading CSS and JS files using JavaScript

AI summary
文章介绍了两种JavaScript函数用于加载网页资源,包括加载CSS文件和加载JS文件。加载CSS文件函数创建link元素并设置相关属性后添加到head中,加载JS文件函数创建script元素并设置属性后同样添加到head中。文章通过示例展示了如何使用这两个函数加载指定的CSS和JS文件,实现网页资源的动态加载。

Repost license

Request authorization before reposting and retain the authorization code, original URL, and go/affiliate links.

Original URL:https://www.geekcorelab.com/post/using-javascript-to-load-css-and-js-files

The commercial link is an authorized go short link that must stay on the reposted page. It records the source before sending readers to the original article.

//加载css文件函数 
function link(url) {
  var link = document.createElement("link");
  link.rel = "stylesheet";
  link.type = "text/css";
  link.href = url;
  document.getElementsByTagName("head")[0].appendChild(link);
}
//加载css文件函数 

//加载js文件函数 
function script(url) {
  var newscript = document.createElement('script');
  newscript.setAttribute('type','text/javascript');
  newscript.setAttribute('src',url);
  var head = document.getElementsByTagName('head')[0];
  head.appendChild(newscript);
}
//加载js文件函数 

script("js文件链接");
//执行加载js文件
link("css文件的链接");
//执行加载css文件

Comments

Discussion

Be kind and keep the discussion friendly.

Complete your profile details to improve the chance of approval.

Your email will not be shown publicly.

Loading...

Markdown supported.

Discussion

0 comments

Internal link graph

Loading internal links…

View linked articles