相互链接的Html脚本src文件



(简化)我有两个javascript文件,我想包括。它们相互连接。

问题:如果我只是包含它们下面有一个错误,因为source1.js需要从source2.js的东西,反之亦然。

我如何在HTML中正确包含相互链接的源文件,而不合并它们?(对各种已经很大的文件进行成像)

<head>
<script src="source1.js"></script>
<script src="source2.js"></script>

source1.js

function filtersomething() {
...
othersource.somefunction();
}

source2.js

var columns = {
text: filtersomething()
}

在工作代码片段中添加了更多内容。我会在这里的一些例子中使用这些代码。它更多地涉及争论、承诺等。我得走了,希望这能帮到你。

您可以在每个JS文件中放置一个事件侦听器,在加载dom之前它不会调用任何函数。这样做允许两个JS文件加载,并看到其他可用的函数。

// script.js
window.addEventListener('DOMContentLoaded', (event) => {
filtersomething();
});
function filtersomething() {
...
othersource.somefunction();
}

因为这些是在script.js之后加载的,所以script2.js总是会看到script.js中可用的内容。所以,script。js不会看到script2.JS有什么,直到它被加载

// script2.js
window.addEventListener('DOMContentLoaded', (event) => {
var columns = {
text: filtersomething()
}
});

也可以像建议的那样监视指针。这在等待jQuery加载时也很有用。因此,在脚本文件中,注意要设置的属性,然后执行。

<head>
<script>
function deferRun(thisMethod, scriptNum) {
if (window[scriptNum])
return thisMethod();
// No property found, set timeout of 50ms and try again
setTimeout(function() { defer(thisMethod, scriptNum) }, 50);     
}
</script>
<script src="source1.js"></script>
<script src="source2.js"></script>
</head>
// script2.JS
// wait until script.js is available, then return result
var columns = {
text: deferRun(filtersomething, 'script1')
}
// Set our window property saying script loaded
window.script2 = true;
//script.js 
function filtersomething() {
...
deferRun(othersource.somefunction, 'script2');
}
// Set our window property saying script loaded
window.script1 = true;

// Think of script1/script2 as <script> tags. 
window.script1 = {
// script.js
filtersomething: () => {
return deferRun('somefunction', 'script2', 'message to use');
}
};
// Now "load" script2.js
window.script2 = {
somefunction: (msg) => {
return `msg response is ${msg}`
},
columns: {
// wait until script.js is available, then return result
text: deferRun('filtersomething', 'script1')
},
render: async() => {
console.log(await window.script2.columns.text);
}
};

(async() => {
await window.script2.render();
})();
<head>
<script>
// this is available to all and before either script 1 or 2 loads
function deferRun(thisMethod, property, argument = null) {
if (window[property])
return window[property][thisMethod](argument);
// No property found, set timeout of 50ms and try again
return setTimeout(function() {
deferRun(thisMethod, property, argument)
}, 50);
}
</script>
</head>
You can place an event listener within each JS file, which would not call any functions until the dom is loaded. Doing this allows both JS files to load in and see the others functions available.
<pre>
// script.js
window.addEventListener('DOMContentLoaded', (event) => {
filtersomething();
});
function filtersomething() {
...
othersource.somefunction();
}
```
Because these are loaded after script.js, script2.js always sees what script.JS has available.  So, script.JS does not see what script2.JS has until after it is loaded 
```js
// script2.js
window.addEventListener('DOMContentLoaded', (event) => {
var columns = {
text: filtersomething()
}
});
```
We can also watch for a pointer, as suggested.  This is useful when waiting for jQuery to load as well. So within your script files, watch for a property to be set, then execute.
</pre>

最新更新