Saturday, May 14, 2016

Inject your JS for the save button click on Publishing Page

I had a requirement to inject my own JavaScript to perform validation before saving the SharePoint page from Edit mode.
I tried lot of option for this for didn’t get any success.
Finally I got a method which solves my problem. The method is WebForm_OnSubmit.
 
Here is the script
 
Paste this code on your Master page if you would like to have this functionality on every SharePoint page. 
Or else you can paste this script on any specific layout page if would like to have this functionality for any specific 
Page layout page.
 
var customJS = WebForm_OnSubmit; 
WebForm_OnSubmit = function () { 
isValid = ValidateYourPage();
if (isValid) return customJS.apply(this, arguments); 
else { 
    $('span[id*=notification_]').remove(); //removes the default save notification 
   return false; 
} 
}
 
function ValidateYourPage(){
        // your code to validate your stuff and should return true/false

}

No comments:

Post a Comment