将链接添加到Sharepoint Suite栏-首次加载失败 [英] Add link to sharepoint suite bar - fail on first load

查看:117
本文介绍了将链接添加到Sharepoint Suite栏-首次加载失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaScript向SharePoint中的套件栏添加自定义链接(前提是SharePoint 2016).该代码工作正常,除了我第一次加载SharePoint网站.在这里:

I'm trying to use JavaScript to add a custom link to the suite bar in SharePoint (SharePoint 2016 on premise). The code works fine except for the first time I load the SharePoint site. Here it is:

<script>
    var raiseFunc = function() {
        var link = document.createElement('a');
        var linktext = document.createTextNode("Google");
        link.href = "http://www.google.ca"
        link.setAttribute("class", "o365button o365cs-nav-appTitle o365cs-topnavText");
        var span = document.createElement('span');
        span.appendChild(linktext);
        span.setAttribute("class", "o365cs-nav-brandingText");
        link.appendChild(span);
        var temp = document.getElementById("Sites_BrandBar");
        temp.parentElement.appendChild(link);
    };

    _spBodyOnLoadFunctions.push(raiseFunc);
</script>

当我刷新页面或浏览时,一切正常.当我从新窗口或新标签页打开页面时,它不起作用. (我已禁用MDS功能.)即使我使用_spbodyOnLoadFunctions技术延迟了代码,但似乎在第一次运行代码时套件栏也不可用.

When I refresh the page or navigate around, everything works. It just doesn't work when I open the page from a new window or tab. (I have the MDS feature disabled.) It seems like the suite bar is not available when the code runs for the first time, even though I'm delaying my code using the _spbodyOnLoadFunctions technique.

有什么想法吗?

更新:根据Matt在评论中的建议,我放入try/catch以获取任何错误.没有错误报告,但是我确实注意到行为发生了变化.我连续进行了约20项测试,有时添加了新链接,有时没有添加,有时又添加了一秒钟,然后消失了.

Update: On Matt's suggestion in the comments, I put in try/catch to get any errors. No errors are reported but I did notice a change in behaviour. I did about 20 tests in a row and sometimes the new link is added fine, sometimes it is not, and sometimes it is added for a second and then disappears.

我还尝试在此行之前的代码中添加警报:

I also tried putting an alert in the code before this line:

var temp = document.getElementById("Sites_BrandBar");

延迟是执行使它工作得如此清楚,这是时机.我假设有时候代码运行时套件栏还没有准备好.我添加了一个超时作为测试:

The delay is execution makes it work so clearly it is a timing thing. I assume that sometimes the suite bar is not ready when the code runs. I added a timeout as a test:

setTimeout(function(){
    var temp = document.getElementById("Sites_BrandBar");
    temp.parentElement.appendChild(link);
}, 500);

这可以解决问题,但是我不喜欢每次页面运行都使用超时的想法.

This fixes the issue but I don't like the idea of using a timeout every time the page runs.

关于如何在套件栏准备就绪之前将代码延迟执行的任何想法?

Any ideas on how to delay the code execution until the suite bar is ready?

推荐答案

根据Matt的评论,此方法有效:

As per Matt's comment, this worked:

尝试将添加链接添加到函数中,并使用SP.SOD.executeFunc('sp.js','SP.ClientContext',yourFunctionToInsertHere);我相信这是在您加载之前未加载的sp.js尝试添加它.这会延迟您的添加,直到加载完成."

"Try to make your add link into a function and use SP.SOD.executeFunc('sp.js','SP.ClientContext', yourFunctionToInsertHere); I believe it's the sp.js that is not loading before you are trying to add it. This should delay your add until it's loaded."

谢谢!

这篇关于将链接添加到Sharepoint Suite栏-首次加载失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆