Tuesday, January 2, 2018

Caching in jQuery getScript function

In SharePoint 2013, there is a jQuery.getScript method which is suggested for loading SP.Runtime.js and SP.js files on the page.

The implementation of jQuery.getScript looks like,

jQuery.getScript(_spPageContextInfo.webAbsoluteUrl+"_layouts/15/sp.runtime.js",function(){
   
      alert("SP.Runtime.js loaded");
});

jQuery.getScript(_spPageContextInfo.webAbsoluteUrl+"_layouts/15/sp.js",function(){
   
      alert("SP.js loaded");
});

Thus, this function came out as a good practice for loading such mandate files on the page but then there is a limitation to this,

1) jQuery.getScript  - fetches the fresh copy of the files every time the page is getting loaded with the current time stamp appended to the file path, because this method disables caching of the browser thus degrading the performance of the page load.

The browsers have a property to cache the file once it's fetched to improve page loads from the next time if the such files are requested.

How to overcome this limitation ?

According to further study on jQuery.getScript method on Microsoft,

I came to a conclusion that jQuery.getScript method calls an ajax function internally which has a "cache" parameter which we can set to "true" if we want the function to look into the browser cache for the requested file or "false" if we want the file to be loaded from the original location.

Now the function looks like,

jQuery.getCachedScript(url,callback){

        jQuery.ajax({
           
             datatype: 'script',
             cache: true,
             url: url,
             success: callback
        });

};

jQuery.getCachedScript(_spPageContextInfo.webAbsoluteUrl+"_layouts/15/sp.runtime.js",function(){
   alert("SP.Runtime got loaded");
});

So, now there is no time-stamp appended to the file path which is fetched on page load as caching is set to 'true'.


Clearing Person/Group Values

To clear out or empty the values in person or group columns, how you do it depends on if the column is single-value or multi-value. For ...