jQuery.getScript, JavaScript ÆÄÀÏÀ» ·ÎµåÇÏ°í ½ÇÇà |
|
|
 |
8³â Àü |
°³¿ä : HTTP GET ¹æ½Ä ¿äûÀ» ÅëÇØ ¼¹ö·ÎºÎÅÍ ¹ÞÀº JavaScript ÆÄÀÏÀ» ·ÎµåÇÏ°í ½ÇÇàÇÕ´Ï´Ù.
jQuery.getJSON( url [, data] [, success(data, textStatus, jqXHR)] )
url Á¤º¸¸¦ ¿äûÇÒ URL
success(data, textStatus, jqXHR) ¿äûÀÌ ¼º°øÇÏ¸é ½ÇÇàµÉ Äݹé ÇÔ¼ö
ÀÌ ÇÔ¼öÀÇ °¡Àå °£´ÜÇÑ »ç¿ë¹ýÀº ¾Æ·¡¿Í °°½À´Ï´Ù.
$.ajax({
url: url,
dataType: "script",
success: success
});
½ºÅ©¸³Æ®°¡ ½ÇÇàµÇ¸é ´Ù¸¥ º¯¼ö¿¡¼ Á¢±ÙÀÌ °¡´ÉÇÏ°í jQuery ÇÔ¼ö¿¡¼µµ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù. Æ÷ÇÔµÈ ½ºÅ©¸³Æ®´Â ÇöÀç ÆäÀÌÁö¿¡ ¿µÇâÀ» ÁÙ ¼ö ÀÖ½À´Ï´Ù.
Success Callback
ÀÌ ÄݹéÇÔ¼ö´Â JavaScript ÆÄÀÏÀ» ¹Ýȯ ¹Þ½À´Ï´Ù. ½ºÅ©¸³Æ®°¡ ÀÌ¹Ì ÀÌ ½ÃÁ¡¿¡¼ ½ÇÇàµÇ¹Ç·Î ÀÌ°ÍÀº ÀϹÝÀûÀ¸·Î À¯¿ëÇÏÁö ¾Ê½À´Ï´Ù.
$(".result").html("<p>Lorem ipsum dolor sit amet.</p>");
½ºÅ©¸³Æ®´Â ÆÄÀÏÀ̸§À» Âü°íÇÑ ÈÄ ·ÎµåÇÏ°í ½ÇÇàµË´Ï´Ù.
$.getScript("ajax/test.js", function(data, textStatus, jqxhr) {
console.log(data); //data returned
console.log(textStatus); //success
console.log(jqxhr.status); //200
console.log('Load was performed.');
});
Handling Errors
jQuery 1.5 ºÎÅÍ .fail() ÇÔ¼ö¸¦ »ç¿ëÇÒ ¼ö ÀÖ°Ô µÇ¾ú½À´Ï´Ù.
$.getScript("ajax/test.js")
.done(function(script, textStatus) {
console.log( textStatus );
})
.fail(function(jqxhr, settings, exception) {
$( "div.log" ).text( "Triggered ajaxError handler." );
});
jQuery 1.5 ÀÌÀü ¹öÁ¯¿¡¼´Â, .ajaxError() Äݹé À̺¥Æ®¿¡ $.getScript() ¿¡·¯ ó¸® ±¸¹®À» Æ÷ÇÔÇؼ »ç¿ëÇØ¾ß ÇÕ´Ï´Ù.
$( "div.log" ).ajaxError(function(e, jqxhr, settings, exception) {
if (settings.dataType=='script') {
$(this).text( "Triggered ajaxError handler." );
}
});
Caching Responses
±âº»ÀûÀ¸·Î $.getScript() ÀÇ cache ¼Ó¼º°ªÀº false ÀÔ´Ï´Ù. ½ºÅ©¸³Æ®¸¦ ¿äû½Ã¿¡ URL¿¡ timestamped ¸¦ Æ÷ÇÔÇÏ¿© ºê¶ó¿ìÁ®°¡ Ç×»ó »õ·Î¿î ½ºÅ©¸³Æ®¸¦ ¿äûÇϵµ·Ï ÇϽʽÿÀ. cache ¼Ó¼ºÀÇ Àü¿ª°ªÀ» »õ·Î ¼¼ÆÃÇÏ·Á¸é $.ajaxSetup()¿¡¼ ÇÏ¼Å¾ß ÇÕ´Ï´Ù.
$.ajaxSetup({
cache: true
});
¿¹ Á¦
ij½ÌµÈ ½ºÅ©¸³Æ®¸¦ °¡Á®¿Ã ¼ö ÀÖµµ·Ï $.cachedScript() ÇÔ¼ö¿¡¼ Á¤ÀÇÇÕ´Ï´Ù.
jQuery.cachedScript = function(url, options) {
// allow user to set any option except for dataType, cache, and url
options = $.extend(options || {}, {
dataType: "script",
cache: true,
url: url
});
// Use $.ajax() since it is more flexible than $.getScript
// Return the jqXHR object so we can chain callbacks
return jQuery.ajax(options);
};
// Usage
$.cachedScript("ajax/test.js").done(function(script, textStatus) {
console.log( textStatus );
});
À½, ¼ÖÁ÷È÷ ¸»¾¸µå·Á¼ À§ ¿¹Á¦°¡ ½ÇÇàµÇ¸é ¾î¶»°Ô µÈ´Ù´Â °ÇÁö Á¤È®ÇÏ°Ô ¸ð¸£°Ú½À´Ï´Ù. :-(
¿¹ Á¦
°ø½Ä jQuery Ä÷¯ ¾Ö´Ï¸ÞÀÌ¼Ç Ç÷¯±×ÀÎ ÆÄÀÏÀ» ·ÎµåÇÏ°í ƯÁ¤ Ä÷¯¸¦ ¹Ý¿µÇÕ´Ï´Ù.
<!DOCTYPE html>
<html>
<head>
<style>
.block {
background-color: blue;
width: 150px;
height: 70px;
margin: 10px;
}</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<button id="go">» Run</button>
<div class="block"></div>
<script>
$.getScript("/scripts/jquery.color.js", function() {
$("#go").click(function(){
$(".block").animate( { backgroundColor: "pink" }, 1000)
.delay(500)
.animate( { backgroundColor: "blue" }, 1000);
});
});
</script>
</body>
</html>
jquery.color.js ÆÄÀÏÀ» ¿¾îº¸¼¼¿ä. ¹«Áö º¹ÀâÇÏ°Ô ¹¹¶ó¹«¶ó µÇ¾î Àֳ׿ä. ±×Áß¿¡ colors = jQuery.Color.names º¯¼ö¿¡ À§ ¿¹Á¦¿¡ ÀÖ´Â blue¿Í pink ¿¡ ´ëÇÑ 16Áø¼ö °ªÀÌ µé¾î ÀÖ½À´Ï´Ù. ±× js ÆÄÀÏÀ» ¿¾î¼ °ü·Ã ·ÎÁ÷À» ¹Ý¿µ½ÃÅ°´Â °ÍÀÔ´Ï´Ù.
|
|
̵̧ : 344 |
̵̧
¸ñ·Ï
|
|