demo4.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function p(s,v) {
document.write(s + " " + v);
document.write("<br>");
}
</script>
<!-- BOM即 浏览器对象模型(Browser Object Model)-->
<!-- 浏览器对象包括-->
<!-- Window(窗口)-->
<!-- Navigator(浏览器)-->
<!-- Screen (客户端屏幕)-->
<!-- History(访问历史)-->
<!-- Location(浏览器地址)-->
<script>
// 一旦页面加载,就会自动创建window对象,所以无需手动创建window对象。
// 通过window对象可以获取文档显示区域的高度和宽度
document.write("文档内容<br>");
document.write("文档显示区域的宽度:" + window.innerWidth);
document.write("<br>");
document.write("文档显示区域的高度:" + window.innerHeight);
document.write("<br>");
</script>
<script>
//获取外部窗体即浏览器的宽度和高度
document.write("外部窗体的宽度:" + window.outerWidth);
document.write("<br>");
document.write("外部窗体的高度:" + window.outerHeight);
document.write("<br>");
</script>
<script>
//打开一个新的窗口
//window.open("");
function openNewWindow(){
newWindow = window.open("");
}
</script>
<button onclick = "openNewWindow()">打开一个新窗口</button>
<br>
<script>
//Navigator即浏览器对象,提供浏览器相关信息
p("浏览器产品名称:" ,navigator.appName);
p("浏览器版本号:",navigator.appVersion);
document.write("<p>浏览器内部代码:");
document.write(navigator.appCodeName + "</p>");
document.write("<p>操作系统:");
document.write(navigator.platform + "</p>")
document.write("<p>是否启用Cookies:" + navigator.cookieEnabled + "</p>");
document.write("<p>浏览器的代理用户报头:" + navigator.userAgent + "</p>");
</script>
<script>
//Screen对象表示用户的屏幕相关信息
//看到的可用区域的高度会比屏幕高度小一点,因为有任务栏的存在
p("用户屏幕分辨率:",screen.width + "*" + screen.height);
p("可用区域大小:",screen.availWidth + "*" + screen.height);
</script>
<script>
//返回上一次访问
function goBack() {
history.back();
}
//返回上上次访问
function goBack2(){
history.go(-2); //-1表示上次,-2表示上上次 ,类推
}
</script>
<button onclick = "goBack()">返回上次访问</button>
<button onclick = "goBack2()">返回上上次访问</button>
<br>
<!-- Location表示浏览器中的地址栏-->
<script>
//reload方法刷新当前页面
var d = new Date();
document.write(d.getHours());
document.write(":");
document.write(d.getMinutes());
document.write(":");
document.write(d.getSeconds());
document.write(":");
document.write(d.getMilliseconds());
function refresh() {
location.reload();
}
</script>
<br>
<button onclick = "refresh()">刷新当前页面</button>
<script>
function jump(){
//跳转到另一个页面
//方法1
//location = "/";
//方法2
location.assign("/");
//assign 会添加记录到浏览历史,点击后退可以返回之前页面
}
</script>
<br>
<button onclick = "jump()">跳转</button>
<br>
<script>
p("协议:",location.protocol);
p("主机号:",location.hostname);
p("端口号(默认是80,没有即表示是80端口):",location.port);
p("主机加端口号:",location.host);
p("访问的路径:",location.pathname);
p("锚点:",location.hash);
p("参数列表:",location.search);
</script>
<!-- 弹出框-->
<script>
//警告框,常用于消息提示 alert
function register(){
alert("注册成功");
}
//确认框 confirm
function del(){
var d = confirm("是否要删除?");
alert(typeof d + " " + d);
}
//输入框 prompt
function input() {
var name = prompt("请输入用户名:");
alert("您输入的用户名是:" + name);
}
</script>
<br>
<button onclick = "register()">注册</button>
<br>
<button onclick = "del()">删除</button>
<br>
<button onclick = "input()">输入用户名</button>
<br>
<!-- 函数setTimeout(functionname, 距离开始时间毫秒数 );-->
<!-- 通过setTimeout在制定的毫秒数时间后,执行一次 函数functionname-->
<script>
function printTime() {
var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();
document.getElementById("time").innerHTML = h + ":" + m + ":" + s;
}
function showTimeInThreeSeconds(){
setTimeout(printTime,3000);
}
</script>
<div id = "time"></div>
<button onclick="showTimeInThreeSeconds()">点击3秒后显示当前时间,并且只显示1次</button>
<br>
<!-- 函数setInterval(函数名, 重复执行的时间间隔毫秒数 );-->
<!-- 通过setInterval重复执行同一个函数,重复的时间间隔由第二个参数指定-->
<p>每隔一秒钟,打印当前时间</p>
<div id = "time2"></div>
<script>
function printTime() {
var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();
document.getElementById("time2").innerHTML = h + ":" + m + ":" + s;
}
var t = setInterval(printTime,1000);
</script>
<br>
<br>
<p>每隔1秒钟,打印当前时间</p>
<div id="time3"></div>
<script>
var t = setInterval(printTime,1000);
function printTime(){
var d = new Date();
var h= d.getHours();
var m= d.getMinutes();
var s= d.getSeconds();
document.getElementById("time3").innerHTML= h+":"+m+":"+s;
if(s%5==0)
clearInterval(t);
}
</script>
<br>
</head>
<body>
</body>
</html>