Home > Web Front-end > JS Tutorial > A simple jquery code for dynamically loading js and css_jquery

A simple jquery code for dynamically loading js and css_jquery

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 16:38:04
Original
1151 people have browsed it

A simple jquery code for dynamically loading js and css, which is used to load some common js and css files through js functions when generating pages.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

//how to use the function below:

//$.include('file/ajaxa.js');$.include('file/ajaxa.css');

//or $.includePath = 'file/';$.include(['ajaxa.js','ajaxa.css']);(only if .js and .css files are in the same directory)

$.extend({

includePath: '',

include: function(file)

{

var files = typeof file == "string" ? [file] : file;

for (var i = 0; i < files.length; i++)

{

var name = files[i].replace(/^\s|\s$/g, "");

var att = name.split('.');

var ext = att[att.length - 1].toLowerCase();

var isCSS = ext == "css";

var tag = isCSS &#63; "link" : "script";

var attr = isCSS &#63; " type='text/css' rel='stylesheet' " : " type='text/javascript' ";

var link = (isCSS &#63; "href" : "src") + "='" + $.includePath + name + "'";

if ($(tag + "[" + link + "]").length == 0) $("head").prepend("<" + tag + attr + link + "></" + tag + ">");

}

}

});

$.include('../js/jquery-ui-1.8.21.custom.min.js');

$.include('../css/black-tie/jquery-ui-1.8.21.custom.css');

Copy after login

Write this function into a common.js file and load the common.js file in html to achieve the goal.
Note:
1. In html5, the

Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template