I had a requirement to enforce the ALT text for an image while
user put any image on content editor web part on a sharepoint page.
For this purpose I need to inject the Javascript while user
trying to save or checkin the page.
While you try to enforce the Alt Text for your image then
you will get lot of sharepoint images. So it’s difficult to identify your image
between them. I tried a lot for that finally I created the array for the
sharepoint OOB images and skip those images to get my image which I added on
content editor web part.
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 = checkAltTextForImage(); if (isValid) return customJS.apply(this, arguments); else { $('span[id*=notification_]').remove(); //removes the default save notification return false; } } function checkAltTextForImage() { var flag = true; if ($('.ms-promotedActionButton')[2].text == "Save") { if ($('img[alt=""]').length > 0) { var image = new Array(); image[0] = "formatmap32x32.png?rev=23"; image[1] = "publishing.png?rev=23"; image[2] = "formatmap16x16.png?rev=23"; image[3] = "analyticsreport32x32.png?rev=23"; image[4] = "SocialTagsAndNotes_32.png?rev=23"; image[5] = "blank.gif?rev=23"; image[6] = "crperspc.gif"; for (var i = 0; i < $('img[alt=""]').length; i++) { if (isExist(image, $('img[alt=""]')[i].nameProp) == -1) { var imageName = $('img[alt=""]')[i].nameProp; alert("There is no alternate text defined for Image " + imageName + ". Please provide the Alternate Text"); flag = false; break; } } } } $('.ms-cui-tb').focus(); return flag; }function isExist(obj, str) { var returnItem = 0; var arr = obj.toString().split(','); if (str.indexOf('?') > -1) { returnItem = 1; } else { for (var i = 0; i < arr.length; i++) { if (arr[i] === str) { returnItem = i; break; } else { returnItem = -1; } } } return returnItem; }
No comments:
Post a Comment