JavaScript教程:JS窗口及输入输出范例


JavaScript是基于对象的脚本编程语言,那么它的输入输出就是通过对象来完成的。其中有关输入可通过窗口(Window)对象来完成,而输出可通过文档(document)对象的方法来实现。

四、范例

下列程序演示了你进入主页所停留的时间。

test7_2.htm

<html>
<form name="myform">
<td vAlign="top" width="135">您在此停留了:
<input name="clock" size="8" value="在线时间"></td>
</form>
<script language="JavaScript">
var id, iM = 0, iS = 1;
start = new Date();
function go()
{
now = new Date();
time = (now.getTime() - start.getTime()) / 1000;
time = Math.floor( time);
iS = time % 60;
iM = Math.floor( time / 60);
if ( iS < 10)
document.myform.clock.value = " " + iM + " 分 0" + iS + " 秒";
else
document.myform.clock.value = " " + iM + " 分 " + iS + " 秒";
id = setTimeout( "go()", 1000);
}
go();
</script>
</body>
</html>

在浏览器中的结果,见图2所示。

图1


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3