JavaScript 教程之JS Window+JS Screen+JS Location+JS History

一.JavaScript Window - 浏览器对象模型

 

浏览器对象模型 (BOM) 使 JavaScript 有能力与浏览器“对话”。

 

浏览器对象模型 (BOM)

浏览器对象模型(Browser Object Model)尚无正式标准。

由于现代浏览器已经(几乎)实现了 JavaScript 交互性方面的相同方法和属性,因此常被认为是 BOM 的方法和属性。

 

 

1.Window 对象

所有浏览器都支持 window 对象。它表示浏览器窗口。

所有 JavaScript 全局对象、函数以及变量均自动成为 window 对象的成员。

全局变量是 window 对象的属性。

全局函数是 window 对象的方法。

甚至 HTML DOM 的 document 也是 window 对象的属性之一:

window.document.getElementById("header");

与此相同:

document.getElementById("header");

 

 

 

 

2.Window 尺寸

有三种方法能够确定浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)。

对于Internet Explorer、Chrome、Firefox、Opera 以及 Safari:

  • window.innerHeight - 浏览器窗口的内部高度
  • window.innerWidth - 浏览器窗口的内部宽度

对于 Internet Explorer 8、7、6、5:

  • document.documentElement.clientHeight
  • document.documentElement.clientWidth

或者

  • document.body.clientHeight
  • document.body.clientWidth

实用的 JavaScript 方案(涵盖所有浏览器):

 

代码

<!DOCTYPE html>
<html>
	<body>

		<p id="demo"></p>

		<script>
			var w = window.innerWidth ||
				document.documentElement.clientWidth ||
				document.body.clientWidth;

			var h = window.innerHeight ||
				document.documentElement.clientHeight ||
				document.body.clientHeight;

			x = document.getElementById("demo");
			x.innerHTML = "浏览器的内部窗口宽度:" + w + ",高度:" + h + "。"
		</script>

	</body>
</html>

 

效果

 

 

3.其他 Window 方法

一些其他方法:

  • window.open() - 打开新窗口

  • window.close() - 关闭当前窗口

  • window.moveTo() - 移动当前窗口

  • window.resizeTo() - 调整当前窗口的尺寸

 

 

 

 

 

 

二.JavaScript Window Screen

 

window.screen 对象包含有关用户屏幕的信息。

 

window.screen 对象在编写时可以不使用 window 这个前缀。

一些属性:

  • screen.availWidth - 可用的屏幕宽度
  • screen.availHeight - 可用的屏幕高度

 

 

1.Window Screen 可用宽度

screen.availWidth 属性返回访问者屏幕的宽度,以像素计,减去界面特性,比如窗口任务栏。

 

代码

<!DOCTYPE html>
<html>
	<body>

		<script>
			document.write("可用宽度:" + screen.availWidth);
		</script>

	</body>
</html>

 

 

效果

 

 

 

2.Window Screen 可用高度

screen.availHeight 属性返回访问者屏幕的高度,以像素计,减去界面特性,比如窗口任务栏。

 

代码

<!DOCTYPE html>
<html>
	<body>

		<script>
			document.write("可用高度:" + screen.availHeight);
		</script>

	</body>
</html>

 

 

效果

 

 

 

三.JavaScript Window Location

 

window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面。

 

window.location 对象在编写时可不使用 window 这个前缀。

一些例子:

  • location.hostname 返回 web 主机的域名
  • location.pathname 返回当前页面的路径和文件名
  • location.port 返回 web 主机的端口 (80 或 443)
  • location.protocol 返回所使用的 web 协议(http:// 或 https://)

 

 

 

1.Window Location Href

location.href 属性 返回当前页面的 URL。

 

代码

<!DOCTYPE html>
<html>
	<body>

		<script>
			document.write(location.href);
		</script>

	</body>
</html>

 

 

效果

 

 

2.Window Location Pathname

location.pathname 属性 返回 URL 的路径名。

 

代码

<!DOCTYPE html>
<html>
	<body>

		<script>
			document.write(location.pathname);
		</script>

	</body>
</html>

 

 

效果

 

 

 

3.Window Location Assign

location.assign() 方法加载新的文档。

 

代码

<!DOCTYPE html>
<html>
	<head>
		<script>
			
			function newDoc(){
				window.location.assign("https://blog.csdn.net/weixin_37730482")
			}
			
		</script>
		
	</head>
	
	<body>

		<input type="button" value="加载新文档" onclick="newDoc()">

	</body>
</html>

 

 

 

效果

 

 

 

 

 

 

 

 

四.JavaScript Window History

 

window.history 对象包含浏览器的历史。

 

window.history 对象在编写时可不使用 window 这个前缀。

为了保护用户隐私,对 JavaScript 访问该对象的方法做出了限制。

一些方法:

  • history.back() - 与在浏览器点击后退按钮相同
  • history.forward() - 与在浏览器中点击按钮向前相同

 

 

 

1.Window History Back

history.back() 方法加载历史列表中的前一个 URL。

这与在浏览器中点击后退按钮是相同的

 

代码

<!DOCTYPE html>
<html>
	
	<head>
		
		<script>
			
			function goBack(){
				document.write(window.history.back());
			}
			
		</script>
		
	</head>
	
	<body>

		<input type="button" value="Back" onclick="goBack()">

	</body>
	
</html>

 

 

 

 

2.Window History Forward

history forward() 方法加载历史列表中的下一个 URL。

这与在浏览器中点击前进按钮是相同的

 

代码

<!DOCTYPE html>
<html>
	<head>
		<script>
			
			function goForward(){
				window.history.forward()
			}
			
		</script>
		
	</head>
	
	<body>

		<input type="button" value="Forward" onclick="goForward()">

	</body>
	
</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值