参考w3school手册全部查手册
window属性
三种弹框
<script>
// 1、警告弹框
// alert('不要出门不要出门!!!')
// 2、确认弹框
// var res = confirm('你确定要抛弃我吗?')
// console.log(res)
// 3、输入弹框
var res = prompt('密码是多少?')
console.log(res)
</script>
window对象方法可以做广告
主要了解如下(重要
)
-
innerWidth窗口文档(内容)宽度
-
innerHeight窗口文档(内容)高度
<scipt> console.log('innerWidth:'+window.innerWidth+',innerHeight:'+window.innerHeight) </scipt>
-
outerWidth浏览器窗口宽度(包括任务栏和滚动条)
-
outerHeight浏览器窗口高度
<scipt> console.log('outerWidth:'+window.outerWidth+',outerHeight:'+window.outerHeight) </scipt>v
-
offsetWidth
:元素(比如div)的宽 -
offsetHeight
:元素(比如div)的高 -
offsetX
: 鼠标位置离盒子
的大小在jQuery中
offset().left -
clientX
:鼠标距离body
的大小在jQuery中
pageX -
offsetLeft
:offsetLeft值的获取跟父级元素没关系,而是跟其上一级的定位元素
(除position:static;外的所有定位如fixed,relative,absolute)有关系。即为相对于定位父级的偏移量
-
offsetTop
offsetTop值的获取跟父级元素没关系,而是跟其上一级的定位元素
(除position:static;外的所有定位如fixed,relative,absolute)有关系。即为相对于定位父级的偏移量
<style type="text/css">
#box{
width: 200px;
height: 200px;
background: orange;
margin: 0 auto;
}
</style>
</head>
<body>
<div id="box"></div>
<script type="text/javascript">
var box=document.getElementById('box');
console.log('offsetHeight:'+box.offsetHeight+'offsetWidth:'+box.offsetWidth)
</script>
</body>
</html>
screenLeft
:窗口左上角距离屏幕左边的距离
screenTop
:窗口左上角距离屏幕上边的距离
scollTop:滚动条距离顶部的距离
window
:表示当前对象
opener
:表示父窗口
<script type="text/javascript">
alert('screenLeft:'+window.screenLeft+',screenTop'+window.screenTop)
</script>
window方法
alert()
警告弹框
confirm()
确认弹框
<script type="text/javascript">
//alert('不要出门')
var res=confirm('你确定?')
console.log(res)//确认为true,取消为false
</script>
prompt
输入弹框
<script type="text/javascript">
var res=prompt('张三?')
console.log()
</script>
open()
打开一个子窗口
重要
先找到对象,在进行操作
父窗口
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body onunload="closeAll()">
<button value="lightblue" onclick="setColor(this)">蓝色</button>
<button value="green" onclick="setColor(this)">绿色</button>
<button value="pink" onclick="setColor(this)">粉色</button>
<button onclick="newWin.child()">调用子窗口的方法</button>
<script type="text/javascript">
// 创建子窗口
var newWin=window.open('test.html','_blank','width=400,height=400,left=300,top=100')
function setColor(obj){
// this代表点击的对象button
console.log(obj)
// 改变本窗口颜色
window.document.bgColor=obj.value;
// 改变新窗口的颜色
newWin.document.bgColor=obj.value;
}
function closeAll(){
// 当关闭这个窗口时,子窗口也要删除
newWin.close();
}
</script>
</body>
</html>
子窗口
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>子窗口</h1>
<button value="lightblue" onclick="setColor(this)">蓝色</button>
<button value="green" onclick="setColor(this)">绿色</button>
<button value="pink" onclick="setColor(this)">粉色</button>
<script type="text/javascript">
function setColor(obj){
// openner返回对创建窗口的引入 即父窗口
// 设置父窗口的颜色
opener.document.bgColor=obj.value;
// 设值当前窗口的颜色
window.document.bgColor=obj.value;
}
function child(){
alert('子窗口方法')
}
</script>
</body>
</html>
moveTo()
移动到指定位置
moveBy()
移动多少
父窗口
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body onunload="closeAll()">
<script type="text/javascript">
var newWin=window.open('test.html','_blank','width=400,height=400,left=0,top=0')
// 移动子窗口和变大子窗口到指定大小
setTimeout(function(){
newWin.moveTo(400,400);
newWin.resizeTo(500,500)
},1500)
</script>
</body>
</html>
子窗口
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>子窗口</h1>
<button value="lightblue" onclick="setColor(this)">蓝色</button>
<button value="green" onclick="setColor(this)">绿色</button>
<button value="pink" onclick="setColor(this)">粉色</button>
<script type="text/javascript">
function setColor(obj){
// openner返回对创建窗口的引入 即父窗口
// 设置父窗口的颜色
opener.document.bgColor=obj.value;
// 设值当前窗口的颜色
window.document.bgColor=obj.value;
}
function child(){
alert('子窗口方法')
}
</script>
</body>
</html>
resizeTo()
变大到指定大小
resizeBy()
变大设置大小
父窗口
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body onunload="closeAll()">
<script type="text/javascript">
var newWin=window.open('test.html','_blank','width=400,height=400,left=0,top=0')
// 每次移动子窗口和变大子窗口到指定大小
setInterval(function(){
newWin.moveBy(40,40);
newWin.resizeBy(50,50)
},1500)
</script>
</body>
</html>
子窗口
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>子窗口</h1>
<button value="lightblue" onclick="setColor(this)">蓝色</button>
<button value="green" onclick="setColor(this)">绿色</button>
<button value="pink" onclick="setColor(this)">粉色</button>
<script type="text/javascript">
function setColor(obj){
// openner返回对创建窗口的引入 即父窗口
// 设置父窗口的颜色
opener.document.bgColor=obj.value;
// 设值当前窗口的颜色
window.document.bgColor=obj.value;
}
function child(){
alert('子窗口方法')
}
</script>
</body>
</html>
scrollTo()
滚动到指定位置
scrollBy()
滚动多少
<script>
for(var i=0;i<1000;i++){
document.write('<h3>疫情严重,大家请保重!!!!</h3>')
}
// setTimeout(function(){
// window.scrollTo(0,3000)
// },1500)
setInterval(function(){
// window.scrollBy(0,100)
},300)
</script>
History对象(非常重要)
forward()
进入下一条历史记录
back()
回到上一条历史记录,登录
后用这个方法
go()
跳转到指定的历史记录
注意
go(-1)
与back()
效果一致
go(1)
与forward()
效果一致
注意
他们前面必须写history
<script type="text/javascript">
console.log(window.history)
setTimeout(function(){
window.history.back();
window.history.go(-1);
},3000)
</script>
location对象
hash
获取#后面的内容
host获取主机名和端口号
hostname获取主机名
pathname返回当前的url路径
protocol返回协议
href获取或设置地址 有历史记录
search
获取?后面的内容
<script>
console.log(window.location)
// host获取主机名和端口名
console.log(window.location.host)//000.0.0.1:8848
// port获取主机名和端口号
console.log(window.location.port)//8848
// hostname获取主机名
console.log(window.location.hostname)//000.0.0.1
// pathname返回当前的url路径
console.log(window.location.pathname)///CQ2108/js%E5%9F%BA%E7%A1%80/0731/04open%E6%89%93%E5%BC%80%E4%B8%80%E4%B8%AA%E6%96%B0%E7%AA%97%E5%8F%A3.html
// protocol返回协议
console.log(window.location.protocol)//http:
// href获取或设置地址 有历史记录
console.log(window.location.href)//一段地址
// hash获取#后的内容
console.log(window.location.hash)
// search获取?后的内容
console.log(window.location.search)
</script>
replace
用于当前页面到注册页面,注册页面到登录,登录直接go(-1)返回当前页面,因为它没有历史记录
assign加载新的页面 有历史记录
<script>
setTimeout(function(){
//window.location.href='test.html';
// assign加载新的页面 有历史记录
//window.location.assign('test.html')
// replace替换为新页面 没有历史记录
window.location.replace('test.html')
},1500)
</script>
n.hash)
// search获取?后的内容
console.log(window.location.search)
```
replace
永远当前页面到注册页面,注册页面到登录,登录直接go(-1)返回当前页面,因为它没有历史记录
assign加载新的页面 有历史记录
<script>
setTimeout(function(){
//window.location.href='test.html';
// assign加载新的页面 有历史记录
//window.location.assign('test.html')
// replace替换为新页面 没有历史记录
window.location.replace('test.html')
},1500)
</script>