While the script works in edit page mode, it fails to run when I exit editing the page.
<script language='javascript' type='text/javascript'>
function FilterOMenu(c, a) {
//Exercise caution when overriding SharePoint core functions, as this code may break with future updates. It currently works with September 2016 CU.
//Default SharePoint function (included above for reference)
if (a == null)
return;
var b = a.tagName == "DIV" ? a.parentNode : a;
//End of default SharePoint
//Custom implementation for sorting filters
var fieldInternalName = a.getAttribute("name");
//Sorting functions
var ascComparer = function(a,b){return a.text<b.text ? -1:a.text>b.text ? 1 : 0;};
var descComparer = function(a,b){return a.text>b.text ? -1:a.text<b.text ? 1 : 0;};
var ascDateComparer = function(a,b){d1 = Date.parse(a.text);d2 = Date.parse(b.text);return d1<d2 ? -1:d1>d2 ? 1 : 0;};
var descDateComparer = function(a,b){d1 = Date.parse(a.text);d2 = Date.parse(b.text);return d1>d2 ? -1:d1<d2 ? 1 : 0;};
var myCustomSort = {};
//Assign field internal name and comparer for sorting
myCustomSort["Kuup_x00e4_ev"] = descDateComparer;
if(typeof c != "undefined" && c != null && typeof myCustomSort[fieldInternalName] != "undefined"){//check if you implemented a custom sort for the current internalname
//Select all checkable items (options)
var allSelectableItems = c.querySelectorAll('[checked]');
if(allSelectableItems.length > 0){
var elementInnerhtmls = [];
var htmlToReplace = "";
var htmlToAppend = "";
for(var i = 0;i<allSelectableItems.length;i++)
{
elementInnerhtmls.push({text: allSelectableItems[i].getAttribute("text"),html:allSelectableItems[i].outerHTML});
htmlToReplace += allSelectableItems[i].outerHTML;
}
elementInnerhtmls = elementInnerhtmls.sort(myCustomSort[fieldInternalName]);
for(var i = 0;i<elementInnerhtmls.length;i++)
{
htmlToAppend += elementInnerhtmls[i].html;
}
//Replace original html with sorted html
c.innerHTML = c.innerHTML.replace(htmlToReplace,htmlToAppend);
}
}
//Default SharePoint function call
OMenu(c, b, null, null, -1)
//End default SharePoint
} </script>
Even though I have set the language and type attributes, the publishing page script only executes while in edit mode. Any ideas why?