Simple Tips and Code to Load External JavaScript, CSS Files Dynamically

This JavaScript article tutorial guides you how to build a simple JavaScript function for loading JavaScript and CSS external files dynamically. No new thing but this JavaScript snippet is still a good way to make your website become more comfortable.

Sampled by ©

Generally we includes an external css ( Cascading Style Sheets ) and js ( javascript ) files in the HEAD section of our HTML page. We include them by following conventional method -

1<link rel="stylesheet" type="text/css" href="stylesheet/mycss.css"/>
2<script language="javascript" src="javascript/myjs.js" type="text/javascript"></script>

This method added files to the page as they encountered in page source code or synchronously.

Here we will learn how to dynamically load an external file in simple steps.

1. For loading .js or .css file dynamically we use DOM methods to first create new “script” or “link” element. Assign it to appropriate attributes and then finally use appendChild() function to include it into head section on document tree.

Example :

01function includejscssfiles(filename, filetype)
03    if (filetype=="js") //if filename is an JavaScript file
04    {
05        var fileref=document.createElement('script')
06        fileref.setAttribute("type","text/javascript")
07        fileref.setAttribute("src", filename)
08    }
09    else if (filetype=="css") //if filename is an CSS file
10    {
11        var fileref=document.createElement("link")
12        fileref.setAttribute("rel", "stylesheet")
13        fileref.setAttribute("type", "text/css")
14        fileref.setAttribute("href", filename)
15    }
16    if (typeof fileref!="undefined")
17    document.getElementsByTagName("head")[0].appendChild(fileref)

2. Now we will create one more function that contains file name and file type. In this function we will call includejscssfiles() by passing file name and file type as its parameters.

1function loadfiles()
3    includejscssfiles('mycss.css', 'css');
4    includejscssfiles('myjs.js', 'js');

3. Call loadfiles function on onload on tag. Thats it.

function parameter “filetype” lets you tell the script what file type to expect before loading. After that, the function sets out to create the element using the appropriate DOM methods, assign it the proper attributes, and finally, add it to the end of the HEAD section.

By referencing the HEAD element of the page first and then calling appendChild(), this means the newly created element is added to the very end of the HEAD tag.

Hope this tutorial will help you.

Translate this page to English Translate this page to French Translate this page to Vietnamese

Recent articles
How to open a car sharing service
Vue developer as a vital part of every software team
Vue.js developers: hire them, use them and get ahead of the competition
3 Reasons Why Java is so Popular
Migrate to Angular: why and how you should do it
The Possible Working Methods of Python Ideology
JavaScript Research Paper: 6 Writing Tips to Craft a Masterpiece
Learning How to Make Use of New Marketing Trends
5 Important Elements of an E-commerce Website
How To Create A Successful Prototype For Your PCB

Top view articles
Top 10 Beautiful Christmas Countdown Timers
65 Free JavaScript Photo Gallery Solutions
Best Free Linux Web Programming Editors
Adding JavaScript to WordPress Effectively with JavaScript Localization feature
Top 10 Best JavaScript eBooks that Beginners should Learn
Top 50 Most Addictive and Popular Facebook mini games
16 Free Code Syntax Highlighters by Javascript For Better Programming
More 30 Excellent JavaScript/AJAX based Photo Galleries to Boost your Sites
Top 10 Free Web Chat box Plug-ins and Add-ons
The Ultimate JavaScript Tutorial in Web Design

Free JavaScript Tutorials & Articles