javascript - 脚本编辑器仅在编辑页面模式下工作

标签 javascript sharepoint sharepoint-2013

当处于编辑页面模式时,脚本可以工作,但是当我停止编辑页面时,脚本将不会运行。

<script language='javascript' type='text/javascript'>
function FilterOMenu(c, a) {
    //Be carefull with overriding SharePoint core functions, for now this will work (September 2016 CU). Ensure you test this every time after installing a SharePoint update.
    //SharePoint default (I placed this above, cause it returns.)
    if (a == null)
        return;
    var b = a.tagName == "DIV" ? a.parentNode : a;
    //End SharePoint default

    //Custom implementation to sort the filter
    var fieldInternalName = a.getAttribute("name");

    //some sort 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 = {};
    //Add the field internal name and give a comparer as value to sort
    //As example the title column as your case is
    myCustomSort["Kuup_x00e4_ev"] = descDateComparer;
    //myCustomSort["Aktiivne"] = ascComparer;
    //console.log(myCustomSort["Date"]);

    if(typeof c != "undefined" && c != null && typeof myCustomSort[fieldInternalName] != "undefined"){//check if you implemented a custom sort for the current internalname
        //select all items that are checkable (the 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 the original html with the sorted html
            c.innerHTML = c.innerHTML.replace(htmlToReplace,htmlToAppend);
        }
    }
    //SharePoint default
    OMenu(c, b, null, null, -1)
    //End SharePoint default
} </script>

我已经设置了语言和类型属性,但仍然没有。 有人可以告诉我为什么我的发布页面脚本仅在我去编辑页面时运行吗?

最佳答案

此问题通常在以下情况下发生:

  • 脚本标记没有 type=”text/javascript”
  • “SP.js”“SP.Runtime” 文件未正确引用。
  • 您没有调用 ExecuteOrDelayUntilScriptLoaded 函数。
  • 最小下载策略功能已激活。
  • 代码写得不正确!

欲了解更多详情,请查看 SharePoint 2016: JSOM is only working in Edit Mode

关于javascript - 脚本编辑器仅在编辑页面模式下工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42090288/

相关文章:

c# - 友好的 URL 与丑陋的查询字符串 URL SharePoint、HTTPModule?

powershell - 如何在Sharepoint 2013中更改版本标签名称

sharepoint-2013 - SharePoint 2013 Visio 文档错误 : the server failed to process the request

javascript - 在某一天隐藏 Sharepoint 2013 页面上的 Web 部件

javascript - rails : Running a script based on values in the database

javascript - OpenLayers 测量 map 渲染时间

sharepoint - 如何在 SharePoint 中以编程方式添加自定义菜单操作?

c# - 在 SharePoint 中以编程方式检查列属于哪个字段类型

javascript - Bootstrap 3列在加载页面时根据窗口大小更改大小

javascript - onbeforeunload 消息出现两次 (Internet Explorer)