[英]How can I ensure that a defer script does not block other scripts (or async executes in order)?
I have a web application that provides a feedback form as provided by Jira.我有一个 web 应用程序,它提供 Jira 提供的反馈表。 Jira provides a snippet of javascript to add so that a popup with a feedback form is generated.
Jira 提供了 javascript 的片段来添加,以便生成带有反馈表单的弹出窗口。
The problem I have is that sometimes Jira is unreachable or very slow, and the result is that my application is made extremely slow to open because of this.我遇到的问题是,有时 Jira 无法访问或非常慢,结果是我的应用程序因此打开非常慢。 The snippet is in this form:
片段是这种形式:
<script type="text/javascript" src="https://url.to.jira/one"></script>
<script type="text/javascript" src="https://url.to.jira/two"></script>
<script type="text/javascript">
window.ATL_JQ_PAGE_PROPS = {
"triggerFunction": function(showCollectorDialog) {
...
}
}
</script>
Now, my first idea was to add "defer", but my own page has its own bunch of javascript to execute.现在,我的第一个想法是添加“延迟”,但我自己的页面有自己的一堆 javascript 来执行。 With "defer", my javascript has to wait until the Jira loading is completed, leaving my page fully rendered, but with my javascript not executed.
使用“延迟”,我的 javascript 必须等到 Jira 加载完成,让我的页面完全呈现,但我的 javascript 没有执行。
With "async", my javascript is loaded and the page is properly displayed even if the Jira code is slow to load, but the order of the Jira scripts is now random.使用“异步”,我的 javascript 已加载,即使 Jira 代码加载缓慢,页面也能正确显示,但 Jira 脚本的顺序现在是随机的。 The two scripts are supposed to be loaded and run in order.
这两个脚本应该按顺序加载和运行。
How can I ensure that the Jira script does not block my page, and also guarantee that the Jira feedback functionality is reliably available?如何确保 Jira 脚本不会阻止我的页面,并保证 Jira 反馈功能可靠可用?
Give the first script the async
attribute, watch for its load
event, and when that occurs, inject the second Jira script manually, so that Jira loads in order while also letting your own code run as soon as it can without waiting for Jira.为第一个脚本提供
async
属性,注意其load
事件,当发生这种情况时,手动注入第二个 Jira 脚本,以便 Jira 按顺序加载,同时让您自己的代码尽快运行,而无需等待 Jira。
<script async src="https://url.to.jira/one"></script>
<script>
const firstJiraTag = document.currentScript.previousElementSibling;
firstJiraTag.addEventListener('load', () => {
document.body.appendChild(document.createElement('script')).src = "https://url.to.jira/two";
// once the above script fires its `load` event, both are loaded
});
// add an `error` listener to firstJiraTag if you want
// put your custom script here
// it will run as soon as this script tag runs,
// without waiting for the Jira above
window.ATL_JQ_PAGE_PROPS = {
"triggerFunction": function(showCollectorDialog) {
...
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.