在我的HTML文档中编写javascript时遇到问题,而在页面完全加载之前没有运行脚本。我已经将代码剥离到非常基本,以试图消除我可能做错的一切。我希望在函数init()
运行之前加载页面并使用window.onload
来执行此操作。但是,函数的警报在页面加载之前运行。无论我将脚本放在头部还是正文中,都会发生这种情况。我有什么不对的?提前谢谢!
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<h1>Green Planet</h1>
<p id="greenplanet">All is well</p>
<h1>Red Planet</h1>
<p >Nothing to report</p>
<h1 >Blue Planet</h1>
<p >All systems A-OK</p>
<script>
function init() {
var planet = document.getElementById("greenplanet");
planet.innerText = "Red Alert: hit by phaser fire!";
planet.setAttribute("class","redtext");
alert("it initialized");
}
window.onload=init;
</script>
</body>
</html>
答案 0 :(得分:0)
你的意思是你希望在执行函数之前完全渲染页面吗?如果是这样,您可以在调用byte[]
时将函数体包裹起来。例如,在执行函数之前,这将完全呈现页面一秒钟:
/**
* Use an order-n de-Bruijn sequence to fill a byte array such that no n-length sub-array is repeated.
*/
public static void trucatedDeBruijnBytes(int n, byte[] arr) {
int written = generateLyndonBytes(1, 1, 256, new byte[n + 1], arr, 0);
if (written != arr.length) {
throw new RuntimeException("Can't generate a unique sequence of length " + arr.length + ", max is " + written);
}
}
private static int generateLyndonBytes(int t, int p, int k, byte[] a, byte[] output, int oidx) {
if (t == a.length) {
if((a.length-1)%p==0) {
int len = Math.min(p, output.length - oidx);
System.arraycopy(a, 1, output, oidx, len);
oidx += len;
}
} else {
a[t] = a[t-p];
assert a[t] < k;
if ((oidx = generateLyndonBytes(t+1,p, k, a, output, oidx)) == output.length) {
return oidx;
}
for(int j = (a[t-p] & 0xFF) + 1; j < k; j++) {
assert(j >= 0 && j < k);
a[t] = (byte)j;
assert a[t] < k;
if ((oidx = generateLyndonBytes(t+1,t, k, a, output, oidx)) == output.length) {
return oidx;
}
}
}
return oidx;
}