<head>
<script type="text/javascript" src="folder/externaljs.js">
</head>
<body onload="someFunctionInExternalJS();">
</body>
我该如何确保首先加载 externaljs.js,以使得someFunctionInExternalJS()
被执行?谢谢!
<head>
<script type="text/javascript" src="folder/externaljs.js">
</head>
<body onload="someFunctionInExternalJS();">
</body>
我该如何确保首先加载 externaljs.js,以使得someFunctionInExternalJS()
被执行?谢谢!
<!doctype html>
<html>
<head>
<script src="my_script.js"></script>
</head>
<body onload="my_function()">
</body>
</html>
function my_function () {
alert("Hello world!");
}
另一个解决方案是将此代码编写到 my_script.js 文件中:
function my_function () {
alert("Hello world!");
}
document.onload = my_function();
$(document).ready(function() {
//anything in here will only be called/work when the document is ready.
//call your function in here, instead of bodyonload
});
window.onload
事件中调用您的 JS 函数。 - frogatto