JS中用于打开、关闭、跳转页面的几个函数用法

window.open('url' , ' _self/_ blank):打开一个页面,属于window的函数

打开的页面对象.close():将打开的页面关闭

window.location.href = ' url ':跳到指定页面

window.history.back():回溯到上一个页面

window.history.go(参数):参数为正:跳到下几个页面;参数为负:跳到上几个页面

举个栗子:

<html>
	<head>
		<meta charset="utf-8" />
		<title>BOM操作</title>
	</head>
	<body>
		<button onclick="openNew();">打开新页面</button>
		<button onclick="closeWindow();">关闭窗口</button>
		<button onclick="tiaoZhuan();">页面跳转</button>
		<button onclick="qianHou();">前后跳转</button>
	</body>
	<script>
		var test = null
		
		function openNew()
		{
			// 打开新页面,需要指定url,打开方式,默认是_blank
			test = window.open('http://www.baidu.com', '_blank')
		}
		function closeWindow()
		{
			// 有些浏览器只能关闭脚本打开的窗口
			// 有些浏览器可以关闭地址栏窗口
			test.close()
		}
		function tiaoZhuan()
		{
			window.location.href = 'http://www.baidu.com'
		}
		function qianHou()
		{
			// 跳转到上个页面
			// window.history.back()
			// 可以实现前后跳转
			// 正数表示向后,负数表示向后
			// go(-1)  <==> back()
			window.history.go(-1)
		}
	</script>
</html>

  

 

posted @ 2018-12-19 14:04  酒沾伊楼  阅读(3551)  评论(0编辑  收藏  举报