window对象
窗口位置
//1.浏览器窗口相对于电脑屏幕左边和上面的位置
var leftPos = (typeof window.screenLeft == "number") ? window.screenLeft : window.screenX;
var topPos = (typeof window.screenTop == "number") ? window.screenTop : window.screenY;
// moveTo() moveBy() 移动浏览器窗口,一般都被浏览器禁用了
窗口大小
outerWidth、outerHeight 浏览器的宽高,包括了页签以及工具栏那些
innerWidth、innerHeight 页面可视区域的宽高,减去页签以及工具栏那些
//获取页面可视区域大小
var pageWidth = window.innerWidth,
pageHeight = window.innerHeight;
if (typeof pageWidth != "number"){
if (document.compatMode == "CSS1Compat"){//标准模式
pageWidth = document.documentElement.clientWidth;
pageHeight = document.documentElement.clientHeight;
} else {
pageWidth = document.body.clientWidth;
pageHeight = document.body.clientHeight;
}
alert("Width: " + pageWidth);
alert("Height: " + pageHeight);
导航和打开窗口
window.open(param1,param2,param3,param4)
param1:URL地址
param2:在哪个窗口框架中打开,也可以是_self、 _parent、 _top 、 _blank
param3:特性字符串、控制新打开窗口的显示(打开的是新窗口,此参数才有效)
param4:是否取代浏览历史中的当前加载页(打开的是当前窗口,此参数才有效)
过程:先判断第二个参数是否有效,无效就去检查第三个参数,第三个参数无效则默认开一个新窗口
let wroxWin = window.open("http://wrox.com/", "topFrame")
等同于
<a href="http://wrox.com/" target="topFrame"></a>
wroxWin.close()//关闭打开的新窗口
wroxWin.opener = null //将新打开的窗口与原窗口断开通信,在独立的进程中运行
系统对话框
alert("")//弹框
comfirm()//确认框
prompt()//问答可输入框
print()//打印
find()//查找
location
1.改变url跳转到新地址
向浏览器历史添加一条新纪录 location.href = 'xxx'
location = ' xxx'
location.assgin('xxx')
不添加新纪录,替换当前记录 location.replace('xxx')
重新加载页面 location.reload() 和浏览器的刷新按钮一样,接口请求浏览器会走缓存判断
location.reload(true) 无论文档的最后修改日期是什么,它都会绕过缓存,从服务器上重新下 载该文档。这与用户在单击浏览器的刷新按钮时按住 Shift 健的效果是完全一样。
history
/**
* 整数 正整数,前进
* 0 , 刷新
* 负整数 , 后退
* url串 在浏览器历史记录中找到相同的url串,进行跳转,没找到什么也不做
*/
history.go()
history.forward() //正整数,前进多少
history.back() //正整数,后退多少