The script editor functions exclusively within the editing page mode

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?

Answer №1

This particular issue tends to arise under the following circumstances:

  • The Script tag lacks the attribute type=”text/javascript”.
  • The files “SP.js” and “SP.Runtime” are not referenced correctly.
  • You forgot to call the ExecuteOrDelayUntilScriptLoaded function.
  • The
    Minimal Download Strategy Feature
    is turned on.
  • The code may have errors in its writing!

For further information, please refer to SharePoint 2016: JSOM is only working in Edit Mode

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way for a server to set up middleware chaining?

Currently, I am facing challenges in grasping the concept behind the ExpressJS module, particularly focusing on the execution of middleware chains. My main goal is to comprehend how a server can listen for incoming requests, and once received, pass the re ...

I am facing a challenge with AngularJS where I am unable to navigate between pages using the

I'm having issues with my route file app.js. Whenever I click on any link or button, it redirects me to books.html. What could be the mistake I'm making? var myApp = angular.module('myApp', ['ngRoute']); myApp.config([&apo ...

How can you make an Angular directive activate only after the scope function in an ng-click handler has been executed?

Scenario I am relatively new to Angular and facing a specific challenge. The goal is to make a directive change the color and add an image to a button. However, I am struggling to get the first if condition to work in my Angular Directive. Issue The ob ...

Steps to link two mat-autocomplete components based on the input change of one another

After reviewing the content on the official document at https://material.angular.io/components/autocomplete/examples I have implemented Autocomplete in my code based on the example provided. However, I need additional functionality beyond simple integrati ...

Three.js - Rotation does not follow local orientation accurately

In my current project, I have created an extensive array of objects centered around a focal point within a scene. My goal is to manipulate these objects along their local axes. Initially, I aligned all the objects to face the origin by using a reference ob ...

Adding information to an HTML table using JQuery

Is there a way to incorporate JSON data into an HTML table? The JSON data follows this format: https://i.stack.imgur.com/lzdZg.png This is the desired HTML table structure: <table class="table table-bordered table-hover "> ...

Can Angular Universal SSR be activated specifically for Googlebot User Agents?

I am aiming to activate Angular Universal SSR only when the User Agent is identified as Googlebot. However, I am uncertain about how to instruct Angular Universal SSR to deliver server side rendered HTML exclusively if the User Agent is Googlebot. server ...

`The Streaming module within React Native`

I am facing an issue in my React Native project where I want to use a node library that depends on stream (require('stream')). The problem arises with the error stream could not be found within the project because stream is a nodejs package not ...

Parsing XML to JSON using JavaScript

I came across this JavaScript function on Stack Overflow that I've been using to convert XML to JSON: function xmlToJson(xml) { try { var obj = {}; if (xml.nodeType == 1) { if (xml.attributes.length > 0) { ...

Issues with IE not recognizing the appendChild() Javascript method

Can someone please help me troubleshoot why this code snippet is not functioning properly in Internet Explorer but works fine in Firefox? I am fairly new to using the appendChild() method. <html> <head> <script type='text/javascript&ap ...

Adding an HTML tag attribute to a Bootstrap 5 popover using the setAttribute() method: A Step-by

Trying to include HTML tags in a popover setAttribute() function within Bootstrap 5, but the tags are displayed as content rather than being applied as attributes. <button type="button" class="btn btn-secondary mx-2" data-bs-containe ...

Completing Forms Automatically with AngularJS

Hello! I'm just starting out with ng and I need to make an autocomplete textbox that will initiate an AJAX call when the text is changed. The catch is that the minimum length required to trigger the AJAX call is 3 characters. However, once the user en ...

Create a list of values from a nested list of objects using JavaScript

I am currently working on compiling a list of specific values. Here are the objects I am dealing with: var data = [ { id: 40, location: 'dhaka', zip: 3244, author: {name: 'jhon', age: 45} }, { id: 41, lo ...

Allow the button to be clicked only when both options from the 1/3 + 1/3 radio buttons are selected

How can I ensure that the next <button> at the bottom of the HTML is only clickable when at least one of each <input type="radio"> is checked? Also, how do I integrate this with my current function? The button itself triggers a jQuery function ...

What is the best way to attach to the beforeSubmit event of a dynamically loaded AJAX form in Yii?

Recently, I encountered an issue with dynamically loading a form via AJAX and attempting to bind the beforeSubmit event. Strangely, the event didn't seem to work as expected, causing the form to submit and the browser to navigate to a new page. This b ...

Exploring ways to loop through objects in a React application

I'm trying to figure out how to modify the code below to iterate through a custom object I have created. function itemContent(number) { return ( <div > <div className="item"> <div className="itemPic& ...

transferring data between react components

I am currently working on breaking down code into smaller components, starting with the snippet below from Rows.jsx. I am struggling to grasp how props are passed between parent and child components. Despite reading several guides and articles, I remain un ...

Definitions that are displayed dynamically when hovering over a particular element

I am seeking a way to implement popup definitions that appear when a div is hovered over. My website showcases detailed camera specifications, and I want users to see a brief definition when they hover over the megapixel class called '.mp'. One o ...

Three.js: Troubleshooting a Black Screen Issue During Rendering

Despite setting the body background color to #135462, the code below is displaying a black screen. It seems like there might be an issue with the rendering code preventing the background color from showing up properly. Additionally, the object is not appe ...

Displaying an array using Javascript that contains variables along with HTML code

First of all, thank you for your help and support! I am struggling with correctly outputting HTML code with variables using jQuery and jQuery Mobile. I receive results from a PHP database, separated by "," and converted into a JavaScript array successfull ...