Примеры для jQuery.getScript()


Определение функции $.cachedScript(), которая разрешает кэширование полученного скрипта:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
jQuery.cachedScript = function( url, options ) {
// Позволяет пользователю установить любую опцию для dataType, cache и url
options = $.extend( options || {}, {
dataType: "script",
cache: true,
url: url
});
// Используем метод $.ajax() так как он более гибкий чем $.getScript
// Возвращаем объект jqXHR для последующей обработки событий
return jQuery.ajax( options );
};
// Пример использования
$.cachedScript( "ajax/test.js" ).done(function( script, textStatus ) {
console.log( textStatus );
});

Динамически загружает плагин jQuery Color Animation и запускает некоторые цветовые анимации после того как функционал плагина будет загружен.

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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.getScript demo</title>
<style>
.block {
background-color: blue;
width: 150px;
height: 70px;
margin: 10px;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<button id="go">&raquo; Запустить</button>
<div class="block"></div>
<script>
var url = "https://code.jquery.com/color/jquery.color.js";
$.getScript( url, function() {
$( "#go" ).click(function() {
$( ".block" )
.animate({
backgroundColor: "rgb(255, 180, 180)"
}, 1000 )
.delay( 500 )
.animate({
backgroundColor: "olive"
}, 1000 )
.delay( 500 )
.animate({
backgroundColor: "#00f"
}, 1000 );
});
});
</script>
</body>
</html>

Демонстрация: