JS-BOM操作-Location、history、常用弹窗、屏幕属性

BOM(Browser Object Model)没有官方的标准,各个浏览器的标准不同,所以会有兼容问题。

一、location和history的常用语句

1、location.href(地址栏的url)

location.href="http://www.baidu.com/"
//将地址栏的url重新赋值为baidu的地址,就可以实现重定向至百度了,类似a标签的效果;

2、location.reload(刷新页面)

location.href=location.href;
//方式1
location.reload();
//方式2

3、history.back(返回上一级页面)

history.back();

4、history.forward(进入下一级页面)

history.forward();

5、histroy.go

history.go(-1);//返回前一个页面
history.go(-2);//返回前两个页面
history.go(1);//进入下一个页面

二、浏览器中常见的弹窗

1、alert(警告窗)

alert("message");//弹出一条警告,只有一个按钮

需要注意的是,alert会阻塞alert后面的代码。

2、confirm(确认窗)

var a = confirm("是否确认删除");//弹出一个确认窗口,点击确认返回一个true,点击取消返回false

3、prompt(弹出输入框)

var password = prompt('请您输入密码');//弹出一个带输入框的窗口,返回值是输入的内容

三、浏览器信息

1、Navigator(浏览器的信息)

navigator可以用来返回浏览器信息,但是兼容性不好

console.log(navigator.userAgent);//可以用来判断用户设备类型以及使用的浏览器种类

2、Screen(屏幕信息)

console.log(screen.width,screen.height);//获取屏幕宽高
console.log(screen.availWidth,screen.availHeight);//获取屏幕宽高(不包含信息栏)
console.log(screenX,screenY);//浏览器与屏幕的距离
console.log(screenLeft,screenTop)//浏览器与屏幕的距离
console.log(innerHeight,innerWidth)//浏览器内容的宽跟高
console.log(outerHeight,outerWidth)//浏览器外部的宽跟高
原文地址:https://www.cnblogs.com/wangzhengxin/p/13217099.html