JS中window常用属性
案例演示:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>window常用属性</title>
<script type="text/javascript">
//超链接和location都能实现页面跳转,
//超链接简单,但location更为灵活。
function boke(){
var b = confirm("您确定要离开本页面吗?");
if(b){
//修改页面的地址
location.href = "https://blog.youkuaiyun.com/coder_boy_";
}
}
function refresh(){
//刷新页面
location.reload();
}
function forward(){
//前进一步
history.forward();
}
function screenSize(){
alert("总宽高:"
+screen.width+","+screen.height);
alert("可用宽高:"
+screen.availWidth+","+screen.availHeight);
}
function llq(){
alert(navigator.userAgent);
}
</script>
</head>
<body>
<input type="button" value="博客"
οnclick="boke();">
<input type="button" value="刷新"
οnclick="refresh();">
<input type="button" value="前进"
οnclick="forward();">
<input type="button" value="屏幕尺寸"
οnclick="screenSize();">
<input type="button" value="浏览器"
οnclick="llq();">
</body>
</html>
最终页面显示效果:
点击“博客”弹出提示框选择是否跳转
点击确定后即可跳转,回到原来页面再点击“前进”会跳转到之前打开的页面
点击“屏幕尺寸”会依次弹出屏幕的实际宽高与可用宽高:
点击“浏览器”会弹出有关浏览器的信息
[浏览器用于 HTTP 请求的用户代理头的值]
知识拓展:
Window.history保存用户在一个会话期间的网站访问记录,用户每次访问一个新的URL即创建一个新的历史记录。
history.go()、history.back()、history.forward()
history.back()和history.forward()分别表示向后一页和向前一页。
history.go(num)表示向前或向后翻多少页,num为正数表示向前翻,为负数则向后翻。
History.back()等价于history.go(-1),history.forward()等价于history.go(1)。