control-freak-ide/Control-Freak-Documentation/jQuery/docs/entries/jQuery.getScript.json
plastic-hub-dev-node-saturn 538369cff7 latest
2021-05-12 18:35:18 +02:00

1 line
4.4 KiB
JSON
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{"name":"jQuery.getScript","type":"method","title":"jQuery.getScript()","deprecated":null,"removed":null,"desc":"Load a JavaScript file from the server using a GET HTTP request, then execute it.","categories":["ajax/shorthand-methods","version/1.0","version/1.5"],"entries":[{"return":"jqXHR","signatures":{"added":"1.0","argument":[{"desc":"A string containing the URL to which the request is sent.","name":"url","type":"String"},{"desc":"A callback function that is executed if the request succeeds.","name":"success(script, textStatus, jqXHR)","optional":"true","type":"Function"}]},"examples":[{"desc":"Define a $.cachedScript() method that allows fetching a cached script:","code":"\njQuery.cachedScript = function( url, options ) {\n\n // Allow user to set any option except for dataType, cache, and url\n options = $.extend( options || {}, {\n dataType: \"script\",\n cache: true,\n url: url\n });\n\n // Use $.ajax() since it is more flexible than $.getScript\n // Return the jqXHR object so we can chain callbacks\n return jQuery.ajax( options );\n};\n\n// Usage\n$.cachedScript( \"ajax/test.js\" ).done(function( script, textStatus ) {\n console.log( textStatus );\n});\n"},{"desc":"Load the <ahref=\"https://github.com/jquery/jquery-color\">official jQuery Color Animation plugin</a> dynamically and bind some color animations to occur once the new functionality is loaded.","code":"\nvar url = \"https://raw.github.com/jquery/jquery-color/master/jquery.color.js\";\n$.getScript( url, function() {\n $( \"#go\" ).click(function() {\n $( \".block\" )\n .animate({\n backgroundColor: \"rgb(255, 180, 180)\"\n }, 1000 )\n .delay( 500 )\n .animate({\n backgroundColor: \"olive\"\n }, 1000 )\n .delay( 500 )\n .animate({\n backgroundColor: \"#00f\"\n }, 1000 );\n });\n});\n","html":"\n<button id=\"go\">&raquo; Run</button>\n<div class=\"block\"></div>\n","css":"\n .block {\n background-color: blue;\n width: 150px;\n height: 70px;\n margin: 10px;\n }\n"}],"longdesc":"\n <p>This is a shorthand Ajax function, which is equivalent to:</p>\n <pre><code>\n$.ajax({\n url: url,\n dataType: \"script\",\n success: success\n});\n </code></pre>\n <p>The script is executed in the global context, so it can refer to other variables and use jQuery functions. Included scripts can have some impact on the current page.</p>\n <h4 id=\"success-callback\">\n Success Callback\n </h4>\n <p>The callback is fired once the script has been loaded but not necessarily executed.</p>\n <pre><code>\n$( \".result\" ).html( \"&lt;p&gt;Lorem ipsum dolor sit amet.&lt;/p&gt;\" );\n </code></pre>\n <p>Scripts are included and run by referencing the file name:</p>\n <pre><code>\n$.getScript( \"ajax/test.js\", function( data, textStatus, jqxhr ) {\n console.log( data ); // Data returned\n console.log( textStatus ); // Success\n console.log( jqxhr.status ); // 200\n console.log( \"Load was performed.\" );\n});\n </code></pre>\n <h4 id=\"handling-errors\">Handling Errors</h4>\n <p>As of jQuery 1.5, you may use <a href=\"/deferred.fail/\"><code>.fail()</code></a> to account for errors:</p>\n <pre><code>\n$.getScript( \"ajax/test.js\" )\n .done(function( script, textStatus ) {\n console.log( textStatus );\n })\n .fail(function( jqxhr, settings, exception ) {\n $( \"div.log\" ).text( \"Triggered ajaxError handler.\" );\n});\n </code></pre>\n <p>Prior to jQuery 1.5, the global <code>.ajaxError()</code> callback event had to be used in order to handle <code>$.getScript()</code> errors:</p>\n <pre><code>\n$( \"div.log\" ).ajaxError(function( e, jqxhr, settings, exception ) {\n if ( settings.dataType == \"script\" ) {\n $( this ).text( \"Triggered ajaxError handler.\" );\n }\n});\n </code></pre>\n <h4 id=\"caching-requests\">Caching Responses</h4>\n <p>By default, <code>$.getScript()</code> sets the cache setting to <code>false</code>. This appends a timestamped query parameter to the request URL to ensure that the browser downloads the script each time it is requested. You can override this feature by setting the cache property globally using <a href=\"/jquery.ajaxsetup/\"><code>$.ajaxSetup()</code></a>: </p>\n <pre><code>\n$.ajaxSetup({\n cache: true\n});\n </code></pre>\n <p>Alternatively, you could define a new method that uses the more flexible <code>$.ajax()</code> method.</p>\n "}]}