简体   繁体   English

在共享点页面上加载SPFX Webpart之前调用Javascript

[英]Javascript getting called before the SPFX Webpart loaded on the sharepoint page

I have created Sharepoint Page with custom masterpage where I have deployed my SPFx Webpart which needs some javascript files. 我创建了具有自定义母版页的Sharepoint页,在该页上已部署了需要一些javascript文件的SPFx Webpart。

Sometimes the Webpart works fine but sometimes not due to my javascript called before SPFx gets loaded on DOM.(I also tried to put javascript in custom masterpage still facing same issue) 有时Webpart可以正常工作,但有时由于我的JavaScript不能在SPFx加载到DOM之前调用。(我也试图将javascript放在自定义母版页中仍然面临相同的问题)

I googled out for the same and Reference 我用谷歌搜索了相同的参考

I did the modification in the javascript function with same by calling function on load event instead of ready function. 我在javascript函数中进行了修改,方法是在load事件而不是ready函数上调用function。 After modification it works fine in chrome browser but it's not working properly in IE and Firefox. 修改后,它在chrome浏览器中可以正常工作,但在IE和Firefox中无法正常工作。

Is there any other way to get the proper outcome. 还有其他方法可以得到正确的结果。

You can control the loading behaviour of external scripts with the attributes async and defer . 您可以使用属性asyncdefer来控制外部脚本的加载行为。

From the MDN web docs : MDN网站文档

defer : This Boolean attribute is set to indicate to a browser that the script is meant to be executed after the document has been parsed, but before firing DOMContentLoaded. defer :此布尔属性设置为向浏览器指示脚本应在文档解析后但在触发DOMContentLoaded之前执行。

async : This is a Boolean attribute indicating that the browser should, if possible, load the script asynchronously. async :这是一个布尔属性,指示浏览器应尽可能异步加载脚本。

Basically, in case, it is just a simple wait, one thing you can do is to wait for DOMContentLoaded . 基本上,以防万一,这只是一个简单的等待,您可以做的一件事就是等待DOMContentLoaded This is a cross-browser event listener that works in firefox and even goes back all the way to ie9. 这是一个跨浏览器的事件侦听器,可在Firefox中使用,甚至可以一直返回到ie9。

 // Option 1 window.addEventListener("DOMContentLoaded", function () { console.log('ready') }, false); 

In case your SPFx code creates the element only later on, you can wait for one of the elements it creates, check for if it exists and only then invokes your function like so: 如果您的SPFx代码稍后才创建该元素,则可以等待它创建的元素之一,检查它是否存在,然后才像下面这样调用您的函数:

 // Option 2 // Wait for the DOM element to be in the DOM const inDom = (selector, timeout = 1000, callback) => { const interval = setInterval(() => { const elem = document.querySelector(selector); if (elem) { clearInterval(interval); // Do stuff callback() } else { console.log('Not in dom') } }, timeout); }; // Wait 3 seconds and add the element to the DOM setTimeout(() => { document.body.innerHTML = document.body.innerHTML + "<div id='test-div'>TEST</div>"; }, 3000); const myAwesomeFunction = () => console.log('Element in dom'); inDom('#test-div', 1000, myAwesomeFunction) 

PS You probably would like to add a fallback in case the element is nowhere to be found after some period, otherwise the interval will run forever. PS您可能想添加一个备用,以防在一段时间后找不到该元素,否则该间隔将永远运行。

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

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