惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - 轻松

css div 垂直居中 How to create custom methods for use in spring security expression language annotations How to check “hasRole” in Java Code with Spring Security? Android 显示/隐藏 应用图标 Android 当媒体变更后,通知其他应用重新扫描 android 脸部抠图 IIS + TOMCAT 注意事项 textbox icon jquery 插件 让IE支持HTML5的Canvas vba 生成euc文件的方法 - 轻松 - 博客园 数值项目的格式化 - 轻松 - 博客园 [原创]让Allvidoes插件支持 优酷(www.youku.com)的视频 ORACLE ERROR CODE代表的意思 http://hi.baidu.com/czh0221/blog/item/9f0447e719644a2ab83820c6.html [转]利用location的hash值来解决Ajax两大难题 JAVA 调用Web Service的方法 xampp-control中启动Apache时80断口被占用的错误 asp.net mail java获取运行的jar(class)文件的路径
Dynamically loading an external JavaScript or CSS file
轻松 · 2013-09-02 · via 博客园 - 轻松

原文:   Dynamically loading an external JavaScript or CSS file

通过javascript动态加载css文件和javascript文件,主要是通过javascript在HTML的Head标签中间插入下面的代码,达到引入css和javascript效果

<link href="SNJStyles.css" type="text/css" rel="stylesheet">

<script src="js/jquery-1.8.3.min.js" type="text/javascript"></script>

实现代码:

 1 function loadjscssfile(filename, filetype){
 2  if (filetype=="js"){ //if filename is a external JavaScript file
 3   var fileref=document.createElement('script')
 4   fileref.setAttribute("type","text/javascript")
 5   fileref.setAttribute("src", filename)
 6  }
 7  else if (filetype=="css"){ //if filename is an external CSS file
 8   var fileref=document.createElement("link")
 9   fileref.setAttribute("rel", "stylesheet")
10   fileref.setAttribute("type", "text/css")
11   fileref.setAttribute("href", filename)
12  }
13  if (typeof fileref!="undefined")
14   document.getElementsByTagName("head")[0].appendChild(fileref)
15 }
16 
17 // 使用例子
18 loadjscssfile("myscript.js", "js") //dynamically load and add this .js file
19 loadjscssfile("javascript.php", "js") //dynamically load "javascript.php" as a JavaScript file
20 loadjscssfile("mystyle.css", "css") ////dynamically load and add this .css file