在Google Chrome扩展开发中使用javascript中的onload()函数时出现空白弹出窗口

Blank popup when using onload() function in javascript with Google Chrome Extension development

本文关键字:函数 窗口 空白 onload 中的 扩展 Chrome Google 开发 javascript      更新时间:2023-09-26

下面是popup.html页面的代码,我使用的是在http://code.google.com/chrome/extensions/getstarted.html

<html>
<head><title>Update Time</title>
<script>
function UpdateTime(){
    var today = new Date();
    var hour = today.getHours();
    var mins = today.getMinutes();
    var secs = today.getSeconds();
    if (secs <=9){
        secs = "0" + secs
    }
    var TotalTime = hour + ":" + mins + ":" + secs;
    if (document.layers) { 
        document.layers.time.document.write(TotalTime); 
        document.layers.time.document.close(); 
    }else if (document.all) { 
        time.innerHTML = TotalTime; 
    } 
    setTimeout("UpdateTime()", 1000) 
}
</script>

</head>
<body onload="UpdateTime()">
<span id=time style="position:absolute;"></span>
</body>
</html>

但我得到的只是一个空白的弹出窗口。。。我做错了什么?需要做些什么才能显示我的javascript?

代替:

if (document.layers) { 
    document.layers.time.document.write(TotalTime); 
    document.layers.time.document.close(); 
}else if (document.all) { 
    time.innerHTML = TotalTime; 
} 

Do:

document.getElementById("time").innerHTML = TotalTime;