【问题标题】:Sharepoint Modern Site - Call JS Function After Page LoadSharepoint 现代网站 - 页面加载后调用 JS 函数
【发布时间】:2021-10-13 06:30:01
【问题描述】:
我已经搜索了解决方案,但只找到了其他版本的 sharepoint 的解决方案。
页面是现代共享点模板,在现代脚本编辑器 webpart 中,我正在加载我的 .js 文件。
有时会调用该函数,有时则不会。刷新页面时,函数会触发。
这些都没用
$(document).ready
window.onload
SP.SOD.executeFunc("sp.js", myFunction);
ExecuteOrDelayUntilScriptLoaded(myFunction, "sp.js");
_spBodyOnLoadFunctionNames.push("myFunction");
有人知道如何在共享点实际加载后触发函数吗?
【问题讨论】:
标签:
javascript
jquery
sharepoint
sharepoint-online
【解决方案1】:
使用 SPFX,您可以选择包含 jQuery,也可以只使用常规浏览器核心 JavaScript API。
我使用以下内容覆盖 SP 现代上的搜索文本框,以便能够使用搜索中心结果页面,因为 Microsoft 忘记了他们在设置页面中提供了搜索中心覆盖 URL 设置,并且实际上并没有在现代中使用它出于某种奇怪的原因,有人应该为此挨个耳光。
/*
Because modern does funky html stuff you'll wanna wait until your specific html element you need to work with is available to be worked with.
*/
$(document).ready(function() {
setTimeout(doesThatDarnSearchBoxExistYetOrWhat, 150);
});
function doesThatDarnSearchBoxExistYetOrWhat() {
var $search = $(".ms-compositeHeader-searchBoxContainer");
if($search === null) {
//Not found, keep waiting.
setTimeout(doesThatDarnSearchBoxExistYetOrWhat, 150);
} else {
// Found, fix that.
getTheModernSpSearchTheHeckOuttaMyFace();
}
}
function getTheModernSpSearchTheHeckOuttaMyFace() {
//Strip events on search form
$(".ms-compositeHeader-searchBoxContainer").html($(".ms-compositeHeader-searchBoxContainer").html())
$form = $(".ms-compositeHeader-searchBoxContainer FORM");
//Change submission to search center results page
$form.attr("action","/sites/Search-Center/Pages/results.aspx")
//Change textbox name to "k" for search param.
$form.find("input[type='search']").attr("name","k")
}