Создал свою функцию подключения файлов теперь я могу:
подключить .JS & .CSS без приставки временной метки в заголовке Request URL
функция include() принимает объект.
Описание объекта:
type - Тип подключаемого файла
root - Путь к папке содержаще подключаемые файлы- Default: в зависимости от type
- script = '/js/'
- link = '/css/'
target - Цель куда будет вставлен новый элементВозможно использовать выборку первого элемента из списка по #id и по .class
cache* - Возможность отключить кеширование файла (при значении false кеширования не будет)
Пример использования:
Кликните здесь для просмотра всего текста
| JavaScript | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| var
list_js = Array('live', 'next'),
list_js2 = Array('next2'),
list_css = Array('buttons', 'buttons2', 'buttons3');
function startInclude(){
include({
type : 'link',
list : list_css,
target : 'head'
});
include({
type : 'script',
list : list_js,
target : 'body'
});
include({
type : 'script', // Тип вставки
list : Array('live', 'next'), // Массив имён файлов
target : '#body', // Вставка скрипта в Элемент по ID
cache : false // Не кешировать скрипт
});
include({
type : 'script', // Тип вставки
list : list_js2, // Массив имён файлов
target : '.metka', // Вставка скрипта в первый Элемент по заданному классу
cache : false
});
};
document.body.onready = startInclude(); |
|
Код функции:
Кликните здесь для просмотра всего текста
| JavaScript | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
| function include( data ) {
var insert, item, cache,
root = data.root || false ,
type = data.type || 'script',
target = data.target || 'head',
arr = data.list,
i = data.list.length,
params = {
'link' : {
rel : "stylesheet",
type : "text/css",
href : ( data.root ) ? root : '/css/'
},
script : {
type : "text/javascript",
src : ( data.root ) ? root : '/js/'
}
} ;
if ( arr.length && ( type == 'link' || type == 'script' ) ) {
for ( i < 0; i--; ) {
item = document.createElement( type );
for( var prop in params[ type ] ) {
item[ prop ] = params[ type ][ prop ];
}
cache = ( data.cache == false ) ? '?_=' + parseInt(new Date().getTime()/1000) : '' ;
if ( type == 'link' ) item.href += arr[ i ] + '.css' + cache;
if ( type == 'script' ) item.src += arr[ i ] + '.js' + cache;
switch( target.charAt(0) ) {
case '#':
insert = document.getElementById( target.replace('#','') );
break;
case '.':
insert = document.getElementsByClassName( target.replace('.','') )[0];
break;
default :
insert = document.getElementsByTagName( target )[0];
}
insert.appendChild( item );
}
} else {
console.log( "Неверный тип ожидаемых данных:", "\nData:", data );
return false;
}
} |
|
* - Кеширование отменяется приставкой временной даты к имени файла при get запросе этого файла. Если Ваш сервер не кеширует файлы, данный скрипт этого не исправит.
|