jQuery provides a function called "getScript", which allows to load external Javascript or js file on the fly. The advantage of using $.getScript is that it loads the content on run time, which is far better than including <script> tag in your head section. jQuery getScript load a JavaScript file from the server using a GET HTTP request, then execute it. The syntax of $.getScript is,
<script src="/js/jsPlugin.js?_=ts2499874935">
instead of,
<script src="/js/jsPlugin.js">
So what it does by appending timestamp is, it tells the browser to get a fresh copy of js file every time. In other words, it disables the cache or it doesn't allow browser to cache the js file. Which can be great sometimes but not always.
What if you want to cache the script, so that it doesn't download every time from the server.
Well, there are 2 ways. First is, before making call to $.getScript method, you can set the cache true for ajax request and set it to false once script is loaded.
Second solution is to modify the default implementation of $.getScript to allow cache. The default implementation of $.getScript is,
Feel free to contact me for any help related to jQuery, I will gladly help you.
//Code Starts $.getScript('url', callback function(){ //call the function here.... }); //Code Ends
- url : A string containing the URL to which the request is sent. It will be URL of your js file.
- callback function : A callback function that is executed if the request succeeds.
//Code Starts $.getScript('js/jsPlugin.js',function(){ Demo(); //This function is placed in jsPlugin.js }); //Code EndsThis is a shorthand Ajax function, which is equivalent to
//Code Starts $.ajax({ url: url, dataType: "script", success: success }); //Code EndsBut do you know what happens internally? When loading external js file using .getScript method, what it does it that it appends a timestamp with every request. So the request may look like,
<script src="/js/jsPlugin.js?_=ts2499874935">
instead of,
<script src="/js/jsPlugin.js">
So what it does by appending timestamp is, it tells the browser to get a fresh copy of js file every time. In other words, it disables the cache or it doesn't allow browser to cache the js file. Which can be great sometimes but not always.
What if you want to cache the script, so that it doesn't download every time from the server.
Well, there are 2 ways. First is, before making call to $.getScript method, you can set the cache true for ajax request and set it to false once script is loaded.
//Code Starts //Set Cache to true. $.ajaxSetup({ cache: true }); $.getScript(urlhere, function(){ //call the function here.... //Set cache to false. $.ajaxSetup({ cache: false }); }); //Code Ends
Second solution is to modify the default implementation of $.getScript to allow cache. The default implementation of $.getScript is,
//Code Starts $.getScript = function(url, callback){ $.ajax({ type: "GET", url: url, success: callback, dataType: "script" }); }; //Code EndsAll you need to do is to add a boolean parameter, which will set the cache attribute to true or false. That's the beauty of jQuery that you can redefine things the way you need.
//Code Starts $.getScript = function(url, callback, cache){ $.ajax({ type: "GET", url: url, success: callback, dataType: "script", cache: cache }); }; //Code EndsSo,now you call the $.getScript like, (notice the 3rd argument)
//Code Starts $.getScript('js/jsPlugin.js',function(){ Demo(); //This function is placed in jsPlugin.js }, true); //Code EndsDon't worry, it will not break your existing code as if jQuery doesn't find the 3rd parameter then it will assign undefined or no to this attribute.
Feel free to contact me for any help related to jQuery, I will gladly help you.
hello, thanks for the post. I am new at jquery and having problem with getscript.
ReplyDeleteI wish to load different adsense/doubleclick scripts from external.js files based on a visitors viewport size.
I have tried testing
$(document).ready(function(){
$.getScript("http://cdn.url/adsense.js");
});
but it doesn't work
Thanks