Project

General

Profile

1
/* Insert Script Plugin
2
 *
3
 *
4
 * Copyright (c) 2008 Kevin Martin (http://synarchydesign.com/insert)
5
 * Licensed under the GPL license:
6
 * http://www.gnu.org/licenses/gpl.html
7
 *
8
 */
9
jQuery.insert = function(file)
10
{
11
	var data	= [];
12
	var data2	= [];
13

    
14
	if (typeof file == 'object')
15
	{
16
		data = file;
17
		file = data.src !== undefined ? data.src : false;
18
		file = file === false && data.href !== undefined ? data.href : file;
19
		file = file === false ? file2 : false;
20
	}
21

    
22
	if (typeof file == 'string' && file.length)
23
	{
24
		var index	= file.lastIndexOf('.');
25
		var index2	= file.replace('\\', '/').lastIndexOf('/') + 1;
26
		var ext		= file.substring(index + 1, file.length);
27
	}
28

    
29
	switch(ext)
30
	{
31
		case 'js':
32
			data2 = {
33
				elm:	'script',
34
				type:	'text/javascript',
35
				src:	file
36
			};
37
		break;
38

    
39
		case 'css':
40
			data2 = {
41
				elm:	'link',
42
				rel:	'stylesheet',
43
				type:	'text/css',
44
				href:	file
45
			};
46
		break;
47

    
48
		default:
49
			data2 = {elm: 'link'};
50
		break;
51
	}
52

    
53
	data2.id = 'script-' + (typeof file == 'string' && file.length ?
54
		file.substring(index2, index) : Math.round(Math.rand() * 100));
55

    
56
	for (var i in data)
57
	{
58
		data2[i] = data[i];
59
	}
60

    
61
	data	= data2;
62
	var tag	= document.createElement(data.elm);
63

    
64
	delete data.elm;
65

    
66
	for (i in data)
67
	{
68
		tag.setAttribute(i, data[i]);
69
	}
70

    
71
	jQuery('head').append(tag);
72

    
73
	return jQuery('#' + data.id);
74
};
(6-6/15)