`
bcyy
  • 浏览: 1942610 次
文章分类
社区版块
存档分类
最新评论

Dynamically loading an external JavaScript or CSS file

 
阅读更多
<head>
<linktype="text/css"rel="stylesheet"href="style/style.css"/>
<scripttype="text/javascript"src="js/jsFile.js"></script>
</head>

We commonly load external javascript (*.js) or css (*.css) file on the page with the aboveway,doesn't we? Actually, we can make use of javascript and DOM implementing dynamically loading those files.We can use operating DOM method to achieve it.For example, load an external javascript file and an external css file with the following function.

functionrequireFile(fileName,fileType)...{
varref;
if(fileType=="js")...{
ref
=document.createElement("script");
ref.setAttribute(
"type","text/javascript");
ref.setAttribute(
"src",fileName);
}

if(fileType=="css")...{
ref
=document.createElement("link");
ref.setAttribute(
"type","text/css");
ref.setAttribute(
"rel","stylesheet");
ref.setAttribute(
"href",fileName);
}

document.getElementsByTagName(
"head")[0].appendChild(ref);
}

Then. we can use requireFile("js/js.js", "js") to dynamically load js/js.js file or requireFile("style/style.css", "css") to dynamically load style.css file.

That's OK!

分享到:
评论

相关推荐

    Dynamically loading classes from DLLs(7KB)

    动态加载DLL(Dynamic Link Library)中的类是一种在运行时按需加载代码的机制,它可以提高程序的灵活性,减少应用程序的启动时间和内存占用。这个压缩包包含的文件是关于如何实现这一功能的源代码示例。...

    Beginning JavaScript with DOM Scripting and Ajax: Second Editon

    Beginning JavaScript with DOM Scripting and Ajax is an essential resource for modern JavaScript programming. This completely updated second edition covers everything you need to know to get up-to-...

    Beginning JavaScript with DOM Scripting and Ajax (pdf + ePub)

    Beginning JavaScript with DOM Scripting and Ajax is an essential resource for modern JavaScript programming. This completely updated second edition covers everything you need to know to get up-to-...

    An Efficient Dynamically Adaptive Mesh for potentially singular solutions

    ### 动态自适应网格技术在潜在奇异性解中的高效应用 #### 摘要与背景 本文介绍了一种高效动态自适应网格生成方法,旨在解决时间依赖问题中出现的复杂结构甚至奇异性行为。该方法的核心是通过求解一组非线性椭圆偏...

    Dynamic Class Loading in the JavaTM Virtual Machine

    Class loaders are a powerful mechanism for dynamically loading software components on the Java platform. They are unusual in supporting all of the following features: laziness, type-safe linkage, user...

    Diode_Switch_Patch.rar_surface

    A reconfigurable antenna is an antenna capable of modifying dynamically its frequency and radiation properties in a controlled and reversible manner.[2] In order to provide a dynamical response, ...

    System.MissingMethodException(解决方案).md

    System.MissingMethodException异常是在运行时,当程序尝试调用的方法不存在时抛出的错误。这种异常通常由于反射使用不当、类型加载问题、程序集未正确加载、第三方库或插件版本不兼容等原因造成。...

    JavaScript in 10 Minutes

    "JavaScript in 10 Minutes" is a concise guide that aims to provide intermediate to advanced JavaScript developers with an overview of the language's unique features and nuances. This document is not ...

    JavaScript APIs HTML5

    The Canvas API allows developers to dynamically render graphics and images on a canvas element. It provides a powerful set of tools for drawing and manipulating images and shapes. The basic setup ...

    Deep Residual Networks with Dynamically Weighted Wavelet Coefficients

    Pecht, “Deep Residual Networks with Dynamically Weighted Wavelet Coefficients for Fault Diagnosis of Planetary Gearboxes,” IEEE Transactions on Industrial Electronics, vol. 65, no. 5, pp. 4290-4300...

    phpmaker610官方安装版

    Server-side validation and/or client-side JavaScript validation Optional search features (Quick, Extended Quick and Advanced) with search result highlight Optional User ID and User Level Advanced ...

    Add Textbox Dynamically using jQuery.zip

    "Add Textbox Dynamically using jQuery.zip"这个压缩包中的示例,主要展示了如何利用jQuery在用户交互时动态创建一个新的文本输入框。 首先,jQuery库是一个强大的JavaScript库,它简化了DOM操作、事件处理、动画...

    2009 达内Unix学习笔记

    集合了 所有的 Unix命令... ln -s file1 file1.sln 创建软链接。可跨系统操作,冲破操作权限;也是快捷方式。 八、时间显示 date 显示时间,精确到秒 用法 date [-u] mmddHHMM[[cc]yy][.SS] date [-u] [+format] ...

    EXECUTABLE AND LINKABLE FORMAT

    The ELF header is a critical part of an ELF file that contains information needed to interpret the file. This includes: - **Magic Number**: A sequence of bytes at the beginning of the file to ...

    Dynamically Centering and Alignment.zip

    在JavaScript的世界里,动态居中和对齐是创建交互式网页和游戏时不可或缺的技术。"Dynamically Centering and Alignment.zip"这个压缩包文件显然包含了关于如何在HTML5 Canvas上实现这一目标的教程或代码示例。...

    javascript日期控件

    - **自定义日历**(2-customized-calendar):开发者可以设计自定义的日历布局,包括样式、颜色、大小等,通过CSS控制视觉效果,并通过JavaScript处理逻辑部分,如日期选择和回显。 3. **功能特性**: - **选择...

    cuteEditor6.0

    &lt;br/&gt;The details of permissions are specified by an XML security policy file. Each level maps to a specific file. The default mappings: &lt;br/&gt;admin设置 admin.config default设置 default....

    Araxis Merge Professional 2012.4162 (x86/x64).part1

    The file comparison display dynamically updates as the merge progresses. Merge shows detailed highlights of changes within lines. It can be configured to ignore differences in whitespace and line ...

Global site tag (gtag.js) - Google Analytics