简体   繁体   English

如何在sharepoint 2013博客(Office365)上使用SyntaxHighlighter?

[英]How to use SyntaxHighlighter on a sharepoint 2013 blog (Office365)?

I'm trying to allow code highlighting using SyntaxHighlighter on a sharepoint 2013 blog site (office365 portal). 我正在尝试在sharepoint 2013博客站点(office365门户网站)上使用SyntaxHighlighter进行代码突出显示。

Here is the code I have put in the head of the masterpage (js and css ressources are loaded before) : 这是我放在母版页头部的代码(之前加载了js和css资源):

<script type="text/javascript">
    function sh(){
        SyntaxHighlighter.highlight();
    };

    // executed when SP load completes
    _spBodyOnLoadFunctionNames.push("sh");
</script>

The _spBodyOnLoadFunctionNames should provide a mechanism to run functions after the load page event, but it seems it's never executed. _spBodyOnLoadFunctionNames应该提供一种在加载页面事件之后运行函数的机制,但它似乎从未执行过。 Running my sh function from the developper tool (console) is working as expected. 从开发工具(控制台)运行我的sh函数正在按预期工作。

Does anybody have a clue, am I using the right event ? 有没有人知道,我使用的是正确的活动吗?

_spBodyOnLoadFunctionNames array declared in init.js (it is a part of SharePoint JavaScript Library) init.js声明的_spBodyOnLoadFunctionNames数组(它是SharePoint JavaScript库的一部分)

According to init.js : 根据init.js

AttachEvent("DOMContentLoaded", _spBodyOnLoadWrapper, document);
window.onload = _spBodyOnLoadWrapper;

where 哪里

function _spBodyOnLoadWrapper() {
    ExecuteOrDelayUntilScriptLoaded(ProcessDefaultOnLoad, "core.js");
    //the remaining code is omitted for clarity..
}


function ProcessDefaultOnLoad() {
    ProcessOnLoadFunctionNames(_spBodyOnLoadFunctionNames);
    //the remaining code is omitted for clarity..
}
function ProcessOnLoadFunctionNames(onLoadFunctionNames) {
    if (onLoadFunctionNames != null) {
        for (var i = 0; i < onLoadFunctionNames.length; i++) {
            var expr = "if(typeof(" + onLoadFunctionNames[i] + ")=='function'){" + onLoadFunctionNames[i] + "();}";

            eval(expr);
        }
        onLoadFunctionNames = [];
    }
}

To summarize, the specified approach is a proper mechanism to run functions after the load page event. 总而言之,指定的方法是在加载页面事件之后运行函数的适当机制。

In fact it works for me just fine (see the picture below) 事实上它对我很有用(见下图)

Make sure init.js library is loaded before _spBodyOnLoadFunctionNames is initialized. 确保在初始化_spBodyOnLoadFunctionNames之前加载了init.js库。

Alternatively you could try the following approach: 或者,您可以尝试以下方法:

<script type="text/javascript">
    function sh(){
        SyntaxHighlighter.highlight();
    };

    ExecuteOrDelayUntilScriptLoaded(sh, "core.js");
</script>

Results 结果

在此输入图像描述

+Vadim Gremyachev's answer is valid with IE, but doesnt work with chrome, here is the workaround I've used (inspirated from https://stackoverflow.com/a/2956980/381149 ): + Vadim Gremyachev的答案对IE有效,但不适用于chrome,这是我使用过的解决方法(来自https://stackoverflow.com/a/2956980/381149 ):

function sh(){
    SyntaxHighlighter.highlight();
};

function setIntervalX(callback, delay, repetitions) {
    var x = 0;
    var intervalID = window.setInterval(function () {

       callback();

       if (++x === repetitions) {
           window.clearInterval(intervalID);
       }
    }, delay);
}

$(document).ready(function () {
    if( $('.syntaxhighlighter').length == 0 ){
        setIntervalX(function() { sh() }, 1000,5);
}

$("a").on("click",function () {
if( $('.syntaxhighlighter').length == 0 ){
    setIntervalX(function() {
      sh()
    }, 1000,5);
}
return true;
});

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM