在header中写document.write,则是在各个控件载入前写入,不会覆盖掉以前的控件们。
<!DOCTYPE html>
<html>
<head>
<title>测试2</title>
<meta charset="utf-8">
<script type="text/javascript">
// header中写入
// setInterval(function (){
document.open(); //外部JS中调用document,则要先写document.open
document.write("<p>" + Date() + "</p>");
document.close(); //用完要关闭,否则后面会不停追加
// },500);
</script>
</head>
<body>
<h3>标题3</h3>
<div style="background-color:orange">
</div>
<div>
<iframe src="http://baidu.com">
</iframe>
</div>
<div>
<iframe id="new_window">
</iframe>
</div>
</p>
<h4 id="idhead"><b style="color: blue;">标题4</b></h4>
<p id="showtxt">
</p>
<script type="text/javascript">
var icount = 0;
function getWindInfo(win) {
return "wid=" + String(win.innerWidth) + " height=" + String(win.innerHeight)
// + " host=" + win.location.host
// + " pathname=" + win.location.pathname
// + " port=" + win.location.port
+ " pageXOffset=" + win.pageXOffset
+ " pageYOffset=" + win.pageYOffset
+ " frameLen=" + win.frames.length
;
}
function showDetail() {
// 获取new_window控件对应的window句柄
var nw = document.getElementById("new_window").contentWindow;
var d = Date();
nw.document.write("<h1>当前\=时间</h1><p>time:<"
+ "<b style=\"color: blue;\">"+ Date() +"</b>"+ "></p>");
// 没有close,就会不停在下一行追加。
nw.document.close();
}
// var timer = setInterval(function () {
// showDetail()
// }, 500);
</script>
</body>
</html>
结果:
body中各个控件创建好了再写document.write,则会把body中的控件抹去。
<!DOCTYPE html>
<html>
<head>
<title>测试2</title>
<meta charset="utf-8">
<script type="text/javascript">
// header中写入
// setInterval(function (){
// document.open(); //外部JS中调用document,则要先写document.open
// document.write("<p>" + Date() + "</p>");
// document.close(); //用完要关闭,否则后面会不停追加
// },500);
</script>
</head>
<body>
<h3>标题3</h3>
<div style="background-color:orange">
</div>
<div>
<iframe src="http://baidu.com">
</iframe>
</div>
<div>
<iframe id="new_window">
</iframe>
</div>
</p>
<h4 id="idhead"><b style="color: blue;">标题4</b></h4>
<p id="showtxt">
</p>
<script type="text/javascript">
var icount = 0;
function getWindInfo(win) {
return "wid=" + String(win.innerWidth) + " height=" + String(win.innerHeight)
// + " host=" + win.location.host
// + " pathname=" + win.location.pathname
// + " port=" + win.location.port
+ " pageXOffset=" + win.pageXOffset
+ " pageYOffset=" + win.pageYOffset
+ " frameLen=" + win.frames.length
;
}
function showDetail() {
// 获取new_window控件对应的window句柄
var nw = document.getElementById("new_window").contentWindow;
var d = Date();
nw.document.write("<h1>当前\=时间</h1><p>time:<"
+ "<b style=\"color: blue;\">"+ Date() +"</b>"+ "></p>");
// 没有close,就会不停在下一行追加。
nw.document.close();
}
var timer = setInterval(function () {
showDetail()
}, 500);
</script>
</body>
</html>